0
votes

I want to open Excel xlsx file without writing path by using variables. I din't know why but it is not working. I have a folder with the main workbook and another one that I want to open that is xlsx. I want to name it UnionWB.

  Private Sub cmdStartMonth_Click()
    'Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    'Analyze month by selecting
    Dim myPath As String
    Dim myFile As String
    Dim UnionWB As Workbook
    Dim MonthName As String
    MonthName = ListMonth.Value
    myExtension = "*.xlsx*"
    Set UnionWB = Workbooks.Open(ThisWorkbook.Path & myExtension)

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    End Sub

Set UnionWB = Workbooks.Open(ThisWorkbook.Path & myExtension)

1
Where is the file on your computer? What is wrong with declaring the folderpath, should it be dynamic?Luuklag
I want it to be dynamic. and the folder names are not in english, I cant use in the code. @LuuklagRafael Osipov
@Luuklag did you understood what I mean afetr the edit?Rafael Osipov
Your code is saying the file name is *.xlsx and not a specific file name. myExtension isn't declared and ThisWorkbook.Path won't have a file separator on the the end - it should be ThisWorkbook.Path & "\" & myExtension. You'll still need to sort out which file it's trying to open though - *.xlsx doesn't refer to a single file. MonthName isn't used anywhere and ScreenUpdating & EnableEvents isn't needed.Darren Bartrup-Cook
What do you mean by "without writing path". ThisWorkbook.Path is a pretty static path - it's wherever the workbook containing the code is saved, so the workbook you're trying to open must be in the same folder as the original file.Darren Bartrup-Cook

1 Answers

2
votes

Here's a couple of examples that may help.

The first will ask you to select the correct file and then open it:

Public Sub Test()

    Dim sWrkbkPath As String
    Dim UnionWB As Workbook

    sWrkbkPath = GetFile(ThisWorkbook.Path)
    If sWrkbkPath <> "" Then
        Set UnionWB = Workbooks.Open(sWrkbkPath)
        MsgBox UnionWB.Name
    End If

End Sub

Function GetFile(Optional startFolder As Variant = -1) As Variant
    Dim fle As FileDialog
    Dim vItem As Variant
    Set fle = Application.FileDialog(msoFileDialogFilePicker)
    With fle
        .Title = "Select your Union Workbook"
        .AllowMultiSelect = False
        .Filters.Add "My Union Workbook", "*.xlsx", 1
        If startFolder = -1 Then
            .InitialFileName = Application.DefaultFilePath
        Else
            If Right(startFolder, 1) <> "\" Then
                .InitialFileName = startFolder & "\"
            Else
                .InitialFileName = startFolder
            End If
        End If
        If .Show <> -1 Then GoTo NextCode
        vItem = .SelectedItems(1)
    End With
NextCode:
    GetFile = vItem
    Set fle = Nothing
End Function

The second method assumes you only have a single xlsx file in the same folder as ThisWorkbook and opens the first file it finds:

Public Sub OpenOnlyXLSXInFolder()
    Dim sWrkbkPath As String
    Dim UnionWB As Workbook
    sWrkbkPath = Dir$(ThisWorkbook.Path & "\*.xlsx")

    'Only expecting a single file so no need to loop.
    If sWrkbkPath <> "" Then
        Set UnionWB = Workbooks.Open(ThisWorkbook.Path & "\" & sWrkbkPath)
        MsgBox UnionWB.Name
    End If

End Sub