Glasgowsmile
Active Member
- Joined
- Apr 14, 2018
- Messages
- 280
- Office Version
- 365
- Platform
- Windows
I have some vba to import a document and I call it using a button but I found if I select nothing and hit 'cancel', it's closing the entire workbook. How do I avoid this?
Code I'm calling:
Code the button is using:
Code I'm calling:
VBA Code:
Sub ImportR()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim ws As Worksheet
Set wkbCrntWorkBook = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa; *.csv"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Set ws = wkbSourceBook.Sheets(1)
With Sheets(1)
.Range("A1:AN112").Copy Sheet2.Range("A1")
End With
End If
ActiveWorkbook.Close SaveChanges:=False
End With
Application.Calculation = xlCalculationAutomatic
End Sub
Code the button is using:
VBA Code:
Private Sub CommandButton1_Click()
Call ImportR
End Sub