Skip if workbook2 not open

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi,can anyone help?

I have a macro that calls a sub and then copies the data to workbook2.
If workbook2 is not open I want to skip a certain part of the code so it doesn't error because workbook2 is closed.

does anyone know of a line of code I can add within the macro to skip a certain part and continue the macro until end sub

Any help would be appreciated

Regards

pwill
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Something like
Code:
   Dim Wbk As Workbook
   
   On Error Resume Next
   Set Wbk = Workbooks("Mybook.xlsm")
   On Error GoTo 0
   If Wbk Is Nothing Then
      MsgBox "not open"
   Else
      MsgBox "Open"
   End If
 
Upvote 0
Thank you Fluff,

I will give this a try when I get home and let you know how I get on

Much appreciated

pwill
 
Upvote 0
Something like
Code:
   Dim Wbk As Workbook
   
   On Error Resume Next
   Set Wbk = Workbooks("Mybook.xlsm")
   On Error GoTo 0
   If Wbk Is Nothing Then
      MsgBox "not open"
   Else
      MsgBox "Open"
   End If



Hi Fluff, I am trying incorporate your code into this macro, could you help?

I have marked where I need the macro to skip

Code:
Private Sub Updt_Click()

Dim wsUpDates As Worksheet: Set wsUpDates = Sheet01
Dim wsDates As Worksheet: Set wsDates = Sheet02
Dim lRowAS As Long
Dim cnt As Long
Dim dts As Long
Dim i As Long
    
    Application.ScreenUpdating = False
    
        Call Clr_Data
        
    dts = tbAddUpdts
        For i = 1 To dts
            Application.Run "UserForm_Archive.CommandButton0_Click"
        Next
    
        Call RowFilter
        
    'if workbook2 not open skip this step
        Call CopyData
        
    'continue Macro from here
    lRowAS = wsUpDates.Cells(Rows.Count, "AS").End(xlUp).Row
    
    cnt = Range("AP4").Value
        For i = 0 To cnt
    
            If wsDates.Range("AP4") = i Then
                wsDates.Range("AK1").Value = "Next Update"
                wsDates.Range("AK2").Value = wsUpDates.Range("AI" & lRowAS).Value
            End If
        Next
        
    'if workbook2 not open skip this step
    Workbooks("Update.xlsm").Sheets("Dates").Range("AK1:AK3").Copy Workbooks("workbook2.xlsm").Sheets("Dates").Range("AK1:AK3")
    
    'continue Macro from here
        With Archive
            .tbUpdts = wsDates.Range("AP4").Value
        End With
    
        Range("D2").Select
    
    Application.ScreenUpdating = True

End Sub

regards

pwill
 
Upvote 0
Try
Code:
Private Sub Updt_Click()

Dim Wbk As Workbook
Dim wsUpDates As Worksheet: Set wsUpDates = Sheet01
Dim wsDates As Worksheet: Set wsDates = Sheet02
Dim lRowAS As Long
Dim cnt As Long
Dim dts As Long
Dim i As Long
    
    Application.ScreenUpdating = False
    
        Call Clr_Data
        
    dts = tbAddUpdts
        For i = 1 To dts
            Application.Run "UserForm_Archive.CommandButton0_Click"
        Next
    
        Call RowFilter
        
    'if workbook2 not open skip this step
        Call Copydata
        
    'continue Macro from here
    lRowAS = wsUpDates.Cells(Rows.Count, "AS").End(xlUp).Row
    
    cnt = Range("AP4").Value
        For i = 0 To cnt
    
            If wsDates.Range("AP4") = i Then
                wsDates.Range("AK1").Value = "Next Update"
                wsDates.Range("AK2").Value = wsUpDates.Range("AI" & lRowAS).Value
            End If
        Next
        
    'if workbook2 not open skip this step
   On Error Resume Next
   Set Wbk = Workbooks("Update.xlsm")
   On Error GoTo 0
   If Not Wbk Is Nothing Then
      Wbk.Sheets("Dates").Range("AK1:AK3").Copy Workbooks("workbook2.xlsm").Sheets("Dates").Range("AK1:AK3")
   End If
    'continue Macro from here
        With Archive
            .tbUpdts = wsDates.Range("AP4").Value
        End With
    
        Range("D2").Select
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks Fluff for your reply,

Not sure whats going wrong but its going to debug with the sub

'Call Copydata'

and if I take that line out it still goes to debug at this line

Wbk.Sheets("Dates").Range("AK1:AK3").Copy Workbooks("workbook2.xlsm").Sheets("Dates").Range("AK1:AK3")

any thoughts why it does this?

pwill

ps I should mention I am keeping workbook2 closed for the purpose of trying this out
 
Last edited:
Upvote 0
Just a thought, I am running this code from a UserForm, would that make any difference?

regards
 
Upvote 0
Thanks Fluff for your reply,

Not sure whats going wrong but its going to debug with the sub

'Call Copydata'

and if I take that line out it still goes to debug at this line

Wbk.Sheets("Dates").Range("AK1:AK3").Copy Workbooks("workbook2.xlsm").Sheets("Dates").Range("AK1:AK3")

any thoughts why it does this?

pwill

ps I should mention I am keeping workbook2 closed for the purpose of trying this out

Thanks for your help Fluff, much apprieciated, it does what I need by just adding 'On Error Resume Next'

ie

Code:
Private Sub Updt_Click()

Dim Wbk As Workbook
Dim wsUpDates As Worksheet: Set wsUpDates = Sheet01
Dim wsDates As Worksheet: Set wsDates = Sheet02
Dim lRowAS As Long
Dim cnt As Long
Dim dts As Long
Dim i As Long
    
    Application.ScreenUpdating = False
    
        Call Clr_Data
        
    dts = tbAddUpdts
        For i = 1 To dts
            Application.Run "UserForm_Archive.CommandButton0_Click"
        Next
    
        Call RowFilter

On Error Resume Next        
    'if workbook2 not open skip this step
        Call Copydata
        
    'continue Macro from here
    lRowAS = wsUpDates.Cells(Rows.Count, "AS").End(xlUp).Row
    
    cnt = Range("AP4").Value
        For i = 0 To cnt
    
            If wsDates.Range("AP4") = i Then
                wsDates.Range("AK1").Value = "Next Update"
                wsDates.Range("AK2").Value = wsUpDates.Range("AI" & lRowAS).Value
            End If
        Next
        
     Set Wbk = Workbooks("Update.xlsm")
On Error Resume Next
    'if workbook2 not open skip this step
      Wbk.Sheets("Dates").Range("AK1:AK3").Copy Workbooks("workbook2.xlsm").Sheets("Dates").Range("AK1:AK3")

    'continue Macro from here
        With Archive
            .tbUpdts = wsDates.Range("AP4").Value
        End With
    
        Range("D2").Select
    
    Application.ScreenUpdating = True

End Sub

regards

pwill
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,688
Members
448,978
Latest member
rrauni

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