Search code examples
hyperlinkreferencecell

Open all excel file from folder based on cell reference-excel vba


I have a following macro which used to open all files based on the value in the cell reference. I am not sure why the below macro is not working.

Sub Macro1()

Dim wb As Workbook
Dim UserSelectedFile As Variant
Dim strFolder As String
Dim strFile As String
        strFolder = Sheets("Snapshot").Range("a12").Value
        strFile = Dir(strFolder & "*.xlsm*")
        Do While strFile <> ""
            Set wb = Workbooks.Open(strFolder & strFile)
            strFile = Dir
        Loop
End Sub

I need to open all the .xlsm files in readonly mode based on link given in cell A12.


Solution

  • Just a string missing in your declaration.

    Sub MacroTest()
    
    Dim wb As Workbook
    Dim FileName As String
    Dim UserSelectedFile As Variant
    Dim strFolder As String
    Dim strFile As String
            FileName = Sheets("Snapshot").Range("A12").Value
            strFile = Dir(strFolder & "*.xlsm*")
            Do While strFile <> ""
                Set wb = Workbooks.Open(strFolder & strFile, ReadOnly:=True)
                strFile = Dir
                DoEvents
            Loop
      End Sub