Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Run a code of the apparent selection only

I am using the below code to open any local file or hyperlink found on my Selection.
The code itself run as expected, But If applied an autoFilter on the sheet and I select manually non continuous cells on the same column,
I found out that my selection contains also the hidden cells in-betweens and subsequently unwanted links are opened.
My question, Is How to modify the below code to run on (includes) the apparent selection only?

Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As LongPtr, _
        ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
        ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
 
Sub OpenDocument2() 'ShellExecute
 
    Dim cel As Range, El, arrCel
 
    For Each cel In Selection.Cells
        If cel.Hyperlinks.Count > 0 Then
            ShellExecute 0, "open", (cel.Hyperlinks(1).Address), "", "", 1
        Else
            arrCel = Split(cel.value, vbLf)
            For Each El In arrCel
                ShellExecute 0, "open", (El), "", "", 1
            Next El
        End If
    Next cel
End Sub

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use

For Each cel In Selection.SpecialCells(xlCellTypeVisible)

which only returns visible, i.e. filtered cells

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading