คำสั่งเปิด file VBA Macro

สอบถามครับว่าตามคำสั่งด้านล่าง แก้อย่างไร ต้องการให้ macro เปิด file เท่านั้น  ห้าม cancel ขอบคุณมากๆครับ (ถ้าไม่เลือก file แล้ว cancel ไป มันจะจบคำสั่งแล้ว macro จะ  error ครับ)



Sub OpenWorkbook_Master()

MsgBox ("Hello. Please select the file master")
Dim fd As FileDialog
Dim fileName As String
Set fd = Application.FileDialog(msoFileDialogOpen)

Dim FileChosen As Integer
FileChosen = fd.Show
fd.Title = "Choose workbook"
fd.InitialFileName = "c:\Documents\"
fd.InitialView = msoFileDialogViewList

fd.Filters.Clear
fd.Filters.Add "Excel workbooks", "*.csv"
fd.Filters.Add "Excel workbooks", "*.xl"
fd.Filters.Add "Excel workbooks", "*.xlsx"
fd.FilterIndex = 1
fd.ButtonName = "Choose this file"
If FileChosen <> -1 Then

MsgBox "No file selected.", vbExclamation, "Sorry!"

Else

fileName = fd.SelectedItems(1)
Workbooks.Open (fileName)

End If


End Sub
คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 2
Sub OpenWorkbook_Master()
    MsgBox ("Hello. Please select the file master")
    Dim fd As FileDialog
    Dim fileName As String
    Set fd = Application.FileDialog(msoFileDialogOpen)

    StartAgain:
    Dim FileChosen As Integer
    FileChosen = fd.Show
    fd.Title = "Choose workbook"
    fd.InitialFileName = "c:\Documents\"
    fd.InitialView = msoFileDialogViewList

    fd.Filters.Clear
    fd.Filters.Add "Excel workbooks", "*.csv"
    fd.Filters.Add "Excel workbooks", "*.xl"
    fd.Filters.Add "Excel workbooks", "*.xlsx"
    fd.FilterIndex = 1
    fd.ButtonName = "Choose this file"
    If FileChosen <> -1 Then
        MsgBox "No file selected.", vbExclamation, "Sorry!"
        GoTo StartAgain
    Else
        fileName = fd.SelectedItems(1)
        Workbooks.Open (fileName)
    End If
End Sub

อันนี้แบบถ้าไม่เลือกไฟล์ ก็จะเปิดหน้าต่างให้เลือกใหม่ครับ หัวเราะ
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่