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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Ok how about
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
   If Not ShtExists(.Name, tsWorkbook) Then
      MsgBox "Sheet doesn't exist"
      Exit Sub
   End If
   .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
and add this to a normal module
VBA Code:
Public Function ShtExists(ShtName As String, Optional wbk As Workbook) As Boolean
    If wbk Is Nothing Then Set wbk = ActiveWorkbook
    On Error Resume Next
    ShtExists = (LCase(wbk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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