i can not open a file with a partial file name on vba

Open Workbook

A Quick Fix

Option Explicit

Sub open_data_workbook()

    Dim myPath As String
    Dim main_workbook As String
    Dim data_workbook As String
    Dim xlsxm As String
    Dim code_name As String
    With ActiveSheet
        code_name = .Range("B" & .Cells(.Rows.Count, 2).End(xlUp).Row).Value
        myPath = ThisWorkbook.Path & "\"
        xlsxm = ".xlsx"
        data_workbook = Dir("*" & code_name & "*" & xlsxm)
        If data_workbook = "" Then
            MsgBox "File not found.", vbCritical, "Fail"
            Exit Sub
        End If
        main_workbook = "EMS-Part Data.xlsm"
    End With
    Application.ScreenUpdating = False
    Workbooks.Open Filename:=myPath & data_workbook
End Sub

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top