Msgbox help

RedOctoberKnight

Board Regular
Joined
Nov 16, 2015
Messages
150
Office Version
  1. 2016
Platform
  1. Windows
Good Morning.

I have the following code that pulls a schedule from another workbook. However, if the schedule in the other workbook hasn't been created yet, I obviously get an error. I'm looking for some help on having a msgbox pop up that states something like "schedule not created yet" if the schedule cant be found. Any help would be much appreciated.

VBA Code:
Public Sub Copy_Range_From_Truck_Schedules()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim tsWorkbook As Workbook

Set tsWorkbook = Workbooks.Open(ThisWorkbook.Path & "\truckschedulesdualsides.xlsx")
With ThisWorkbook.ActiveSheet
.Range("A1:Q100").UnMerge
tsWorkbook.Worksheets(.Name).Range("A1:Q100").Copy
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
.Range("A1").PasteSpecial xlPasteColumnWidths
.Range("C:D").EntireColumn.AutoFit
.Range("F:H").EntireColumn.AutoFit
.Range("K:M").EntireColumn.AutoFit
.Range("O:Q").EntireColumn.AutoFit
.Range("N:N").EntireColumn.Hidden = True
End With
tsWorkbook.Close False
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about
VBA Code:
Dim tsWorkbook As Workbook
Dim FullNme As String
FullNme = ThisWorkbook.Path & "\truckschedulesdualsides.xlsx"
If Dir(FullNme) = "" Then
   MsgBox "Not created"
   Exit Sub
End If
Set tsWorkbook = Workbooks.Open(FullNme)
With ThisWorkbook.ActiveSheet
 
Upvote 0
Sorry, self taught user and I'm still new to VBA. Do I just copy and paste that over my old code? or how do I go about integrating that into my code?
 
Upvote 0
Just add the part in blue to your code as shown and change your code as shown by the part in red
Rich (BB code):
Dim tsWorkbook As Workbook
Dim FullNme As String
FullNme = ThisWorkbook.Path & "\truckschedulesdualsides.xlsx"
If Dir(FullNme) = "" Then
   MsgBox "Not created"
   Exit Sub
End If
Set tsWorkbook = Workbooks.Open(FullNme)
With ThisWorkbook.ActiveSheet
 
Upvote 0
So this is how I put it together but I don't receive a msgbox and I get an error with "tsWorkbook.Worksheets(.Name).Range("A1:Q100").Copy"


VBA Code:
Public Sub Copy_Range_From_Truck_Schedules()

 Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim tsWorkbook As Workbook
Dim FullNme As String
FullNme = ThisWorkbook.Path & "\truckschedulesdualsides.xlsx"
If Dir(FullNme) = "" Then
MsgBox "Not created"
Exit Sub
End If

Set tsWorkbook = Workbooks.Open(FullNme)
With ThisWorkbook.ActiveSheet
.Range("A1:Q100").UnMerge
tsWorkbook.Worksheets(.Name).Range("A1:Q100").Copy
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
.Range("A1").PasteSpecial xlPasteColumnWidths
.Range("C:D").EntireColumn.AutoFit
.Range("F:H").EntireColumn.AutoFit
.Range("K:M").EntireColumn.AutoFit
.Range("O:Q").EntireColumn.AutoFit
.Range("N:N").EntireColumn.Hidden = True
End With
tsWorkbook.Close False
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub
 
Upvote 0
In that case you wont get the message box as the file exists.
 
Upvote 0
sorry, I should be more specific and tell you that the "truckschedules" has multiple sheets that have dates for sheet names. Currently, my code opens up the truckschedules, looks for the same sheet name as the current sheet and copies the data to that sheet. I want it to open up the truckschedules and if the truckschedules don't have a sheet with the same name as the current sheet, then give the "not created" msg box. If the truckschedules do have a sheet with the same name as the current sheet, then copy the data over to the current sheet.
 
Upvote 0
So you want to see if a sheet exists, rather then the workbook?
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top