Invalid Qualifier

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
Could somebody please explain what I have got wrong with this bit of code.
I am trying to search through folders & subfolders for a folder named the same as the unique value in the selected range.
Once it finds the folder I then want to insert a hyperlink for that folder in the corresponding value in the selected range, then next until all the selected cells are hyperlinked to the relevant folders
I am getting the error "Invalid Qualifier" & it’s highlighting "Filepath" in this line of code
VBA Code:
For Each SubFolder In FilePath.SubFolders
In testing stage at present, so I'm not sure if it actually works yet as I can't get past this error, MS Help totally confused me.
Full code below
All help as always appreciated
VBA Code:
Sub InsertHyperlinks() 'need to get it to search subfolders
    Dim FilePath As String
    Dim FldrName As String
    Dim c As Range
   
 Selection.Hyperlinks.Delete ' clearing any old hyperlinks from selected cells

FilePath = "U:\QC Document Controller\Test\"
    For Each SubFolder In FilePath.SubFolders
        For Each c In Selection
            FldrName = (c.Value)
                If FldrName <> "" Then
                    ActiveSheet.Hyperlinks.Add Anchor:=c, Address:=FilePath & c.Value, TextToDisplay:=c.Value
            End If
        Next c
    Next
End Sub
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You're trying to use FileSystemObject properties and yet you haven't declared it. Have you enabled microsoft scripting runtime? On top You haven't declared a folder object that will used to loop through each subfolder. This is how you loop through every subfolder in a folder.

VBA Code:
Sub worker()
         Dim FSO As object
         Set FSO = CreateObject("Scripting.FileSystemObject")
         Dim pather As String
         pather = "E:\Softwares" 'change path to test it yourself
         Dim Fol As Folder
         Dim FolLooker As Folder
         Set Fol = FSO.GetFolder(pather)
         For Each FolLooker In Fol.SubFolders
            MsgBox FSO.GetFileName(FolLooker)
        Next FolLooker

End Sub
 
Upvote 0
Thanks so much for your help
I tried your code "worker" which ran ok but only goes to the first subfolder I need to go deeper & search all subfolders
I have attempted to rewrite my code using what you sent me
On the line of code where I am trying to use the values from the selected cells as the filenames to search for. I get a compile error “Invalid use of a property” highlighting FolLooker, in line of code below, I also tried Fol, which gave the same error
VBA Code:
FolLooker = (c.Value)
I have also used FolLooker within the code just below that & where I am trying to get it to hyperlink, which I am not convinced is going to be OK
How do I take this code to the next step and search through folders & subfolders & Subfolders of Subfolders for a folder named the same as the unique values in the selected range and then insert a hyperlink to that folder in the corresponding cell in the selected range, then next until all the selected cells are hyperlinked to the relevant folders.
Any further help would be appreciated
Full code below
VBA Code:
Sub InsertHyperlinks() 'I have enabled microsoft scriping runtime in the libary
    Dim FSO As New Scripting.FileSystemObject
    Dim pather As String
    pather = "U:\QC Document Controller\Test\"
    Dim Fol As Folder
    Dim FolLooker As Folder
    Set Fol = FSO.GetFolder(pather)
    Dim c As Range
    
 Selection.Hyperlinks.Delete ' clearing any old hyperlinks from selected cells

For Each FolLooker In Fol.SubFolders
    For Each c In Selection
        FolLooker = (c.Value)
                If FolLooker <> "" Then
                    ActiveSheet.Hyperlinks.Add Anchor:=c, Address:=FolLooker & c.Value, TextToDisplay:=c.Value
            End If
        Next c
    Next FolLooker
End Sub
 
Upvote 0
Give this one a try

VBA Code:
Sub FolderSearcher(ByVal Pather As String)
        Dim FSO As New Scripting.FileSystemObject
        Dim Fol As Folder
        Dim Fol2 As Folder
        Dim rng As Range, rng2 As Range
        Set Fol = FSO.GetFolder(Pather)
        Set rng = Selection
        For Each Fol2 In Fol.SubFolders
                For Each rng2 In rng
                        If LCase(rng2.Value) = LCase(FSO.GetFileName(Fol2)) Then
                            Range("A1").Hyperlinks.Add rng2.Offset(0, 1), Fol2
                        End If
                Next rng2
                FolderSearcher Fol2
        Next Fol2
        
        
End Sub


Sub searcher()
            
            FolderSearcher "C:\Users\Windows\Desktop\Test"      'just enter the fil path here and press F5, you have to run the macro from here not the above one.
                
            
End Sub
 

Attachments

  • 1674034572130.png
    1674034572130.png
    79.9 KB · Views: 5
Upvote 0
Solution
That is Brilliant, just modified the offset so it added the hyperlink to the selected range in column A, now exactly what I was trying to achieve.
Sub searcher()

VBA Code:
            FolderSearcher "U:\QC Document Controller\Test\"     'just enter the file path here and press F5, you have to run the macro from here not the above one.
                
            
End Sub

Sub FolderSearcher(ByVal Pather As String)
        Dim FSO As New Scripting.FileSystemObject 'I have enabled microsoft scriping runtime in the references
        Dim Fol As folder
        Dim Fol2 As folder
        Dim rng As Range, rng2 As Range
        Set Fol = FSO.GetFolder(Pather)
        Set rng = Selection
        For Each Fol2 In Fol.subfolders
                For Each rng2 In rng
                        If LCase(rng2.Value) = LCase(FSO.GetFileName(Fol2)) Then
                            Range("A1").Hyperlinks.Add rng2.Offset(0, 0), Fol2
                        End If
                Next rng2
                FolderSearcher Fol2
        Next Fol2
        
        
End Sub

Thank you so much
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top