简体   繁体   中英

how to avoid runtime error while displaying a message box in excel vba

I am trying to display a message box saying "file not chosen" if a file is not chosen.my problem is the message box is getting displayed but when i click on ok then i get an runtime error 1004 saying "file not found or check the spelling and location of the file".can anyone help me how to avoid this error.thank you

Dim Wbk1 As Workbook, Wbk2 As Workbook, Wbk3 As Workbook
Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
Dim tmp1 As String, tmp2 As String, tmp3 As String
Dim TextBox1 As String
Dim TextBox2 As String
Dim TextBox3 As String


tmp1 = Sheets("Sheet1").TextBox1.Value
If Len(Trim(tmp1)) = 0 Then
MsgBox "file not chosen"
End If
Exit sub
tmp2 = Sheets("Sheet1").TextBox2.Value
If Len(Trim(tmp2)) = 0 Then
MsgBox "destination file not selected"
End If
Exit sub
tmp3 = Sheets("Sheet1").TextBox3.Value
If Len(Trim(tmp3)) = 0 Then
MsgBox "mapping file not selected"
End If
Exit sub


Set Wbk1 = Workbooks.Open(tmp1)
Set Wbk2 = Workbooks.Open(tmp2)
Set Wbk3 = Workbooks.Open(tmp3)

Set Sh1 = Wbk1.Sheets("Inventory")
Set Sh2 = Wbk2.Sheets("Inventory")
Set Sh3 = Wbk3.Sheets("Sheet1")

The snipit of code you provided worked fine for me so I am going to assume that you have additional code in this routine that continues to run after you click 'ok' in the msgbox. That code most likely is trying to access a filename = nothing resulting in the error message your are receiving. Based on this assumption why not just add:

exit sub

folling your msgbox code.

As John mentioned, you have to use the Exit For . But you are using them at the wrong place.

Also it is a bad idea to declare the control as a variable. Yes, I am referring to

Dim TextBox1 As String
Dim TextBox2 As String
Dim TextBox3 As String

Try this

Sub Sample()
    Dim Wbk1 As Workbook, Wbk2 As Workbook, Wbk3 As Workbook
    Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
    Dim tmp1 As String, tmp2 As String, tmp3 As String

    tmp1 = ThisWorkbook.Sheets("Sheet1").TextBox1.Value

    If Len(Trim(tmp1)) = 0 Then
        MsgBox "file not chosen"
        Exit Sub
    End If

    tmp2 = ThisWorkbook.Sheets("Sheet1").TextBox2.Value

    If Len(Trim(tmp2)) = 0 Then
        MsgBox "destination file not selected"
        Exit Sub
    End If

    tmp3 = ThisWorkbook.Sheets("Sheet1").TextBox3.Value

    If Len(Trim(tmp3)) = 0 Then
        MsgBox "mapping file not selected"
        Exit Sub
    End If

    Set Wbk1 = Workbooks.Open(tmp1)
    Set Wbk2 = Workbooks.Open(tmp2)
    Set Wbk3 = Workbooks.Open(tmp3)

    Set Sh1 = Wbk1.Sheets("Inventory")
    Set Sh2 = Wbk2.Sheets("Inventory")
    Set Sh3 = Wbk3.Sheets("Sheet1")
End Sub

After doing all this, if you still get the message "file not found or check the spelling and location of the file" then that means that the path mentioned in the textbox is not correct. You can actually use DIR to check if the path is correct or not.

For example

 If Dir(tmp1) = "" Then
     Msgbox "Incorrect Path/File. Please ensure that the Textbox Has correct path"
 Else
     Set Wbk1 = Workbooks.Open(tmp1)
 End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM