คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 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
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
อันนี้แบบถ้าไม่เลือกไฟล์ ก็จะเปิดหน้าต่างให้เลือกใหม่ครับ

แสดงความคิดเห็น
คำสั่งเปิด file VBA Macro
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