VBA loop copy & paste range to same worksheet name of another workbook n.2

AriannaVV

New Member
Joined
Aug 6, 2017
Messages
34
Office Version
  1. 365
Platform
  1. Windows
I found here the code below which does exactly what I need and that is loop through sheets of a source workbook, copy a range of data and paste to another workbook where sheets have the same name of the source wb. Works perfectly until it comes to different named sheets where I get an error message "out of range". How can I skip this? Furthermore there are two sheets (TOTALS & GENERAL) that I want the code to skip them and run only through all other sheets. Thank you in advance for your kind help.
VBA Code:
Sub Button1_Click()

    Dim SourceWb As Workbook, DestWb As Workbook
    Dim SourceWs As Worksheet, DestWs As Worksheet
    Dim WsName As String
    Dim CopyRng As Range


    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
   
    Set SourceWb = ThisWorkbook
    'Set SourceWs = SourceWb.Worksheets
   
    Set DestWb = Workbooks.Open("C:\Users\Juanna\Desktop\downloads\master.xlsx", , True) 'Readonly = True
   
    'Loop through all worksheets and copy the data to the DestWs
    For Each SourceWs In SourceWb.Worksheets
   
        'Fill in the range that you want to copy
        Set CopyRng = SourceWs.Range("b1:c10")
       
        Set DestWs = DestWb.Worksheets(SourceWs.Name)
       
        With CopyRng
        DestWs.Cells(Last + 1, "b").Resize(.Rows.Count, .Columns.Count).Value = .Value
        End With
Next
ExitTheSub:
    Application.Goto DestWs.Cells(1)
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,
untested but see if this update to your code does what you want

VBA Code:
Sub Button1_Click()
    
    Dim SourceWb    As Workbook, DestWb As Workbook
    Dim SourceWs    As Worksheet, DestWs As Worksheet
    Dim WsName      As String
    Dim CopyRng     As Range
    
    '-----------------------------------------------------------------------------------------------------------------------------------
    '                                                               SETTINGS
    '-----------------------------------------------------------------------------------------------------------------------------------
    'destination filepath & filename
    Const strFileName = "C:\Users\Juanna\Desktop\downloads\master.xlsx"
    
    'sheets to ignore
    IgnoreSheets = Array("TOTALS", "GENERAL")
    
    '-----------------------------------------------------------------------------------------------------------------------------------
    
    On Error GoTo ExitTheSub
    With Application
        .ScreenUpdating = False: .EnableEvents = False
    End With
    
    Set SourceWb = ThisWorkbook
    'Set SourceWs = SourceWb.Worksheets
    
    Set DestWb = Workbooks.Open(strFileName, , True)        'Readonly = True
    
    'Loop through all worksheets and copy the data to the DestWs
    For Each SourceWs In SourceWb.Worksheets
        If IsError(Application.Match(SourceWs.Name, IgnoreSheets, 0)) Then
            'Fill in the range that you want to copy
            Set CopyRng = SourceWs.Range("b1:c10")
            
            Set DestWs = DestWb.Worksheets(SourceWs.Name)
            
            With CopyRng
                DestWs.Cells(1, "b").Resize(.Rows.Count, .Columns.Count).Value = .Value
            End With
        End If
        
nextsheet:
        Set DestWs = Nothing
        Set CopyRng = Nothing
    Next
    
ExitTheSub:
    If Err <> 0 Then
        If Err.Number = 9 Then Resume nextsheet
        MsgBox (Error(Err)), 48, "Error"
    End If
    
    With Application
        .ScreenUpdating = True: .EnableEvents = True
    End With
End Sub

Dave
 
Upvote 1
Hi,
untested but see if this update to your code does what you want

VBA Code:
Sub Button1_Click()
   
    Dim SourceWb    As Workbook, DestWb As Workbook
    Dim SourceWs    As Worksheet, DestWs As Worksheet
    Dim WsName      As String
    Dim CopyRng     As Range
   
    '-----------------------------------------------------------------------------------------------------------------------------------
    '                                                               SETTINGS
    '-----------------------------------------------------------------------------------------------------------------------------------
    'destination filepath & filename
    Const strFileName = "C:\Users\Juanna\Desktop\downloads\master.xlsx"
   
    'sheets to ignore
    IgnoreSheets = Array("TOTALS", "GENERAL")
   
    '-----------------------------------------------------------------------------------------------------------------------------------
   
    On Error GoTo ExitTheSub
    With Application
        .ScreenUpdating = False: .EnableEvents = False
    End With
   
    Set SourceWb = ThisWorkbook
    'Set SourceWs = SourceWb.Worksheets
   
    Set DestWb = Workbooks.Open(strFileName, , True)        'Readonly = True
   
    'Loop through all worksheets and copy the data to the DestWs
    For Each SourceWs In SourceWb.Worksheets
        If IsError(Application.Match(SourceWs.Name, IgnoreSheets, 0)) Then
            'Fill in the range that you want to copy
            Set CopyRng = SourceWs.Range("b1:c10")
           
            Set DestWs = DestWb.Worksheets(SourceWs.Name)
           
            With CopyRng
                DestWs.Cells(1, "b").Resize(.Rows.Count, .Columns.Count).Value = .Value
            End With
        End If
       
nextsheet:
        Set DestWs = Nothing
        Set CopyRng = Nothing
    Next
   
ExitTheSub:
    If Err <> 0 Then
        If Err.Number = 9 Then Resume nextsheet
        MsgBox (Error(Err)), 48, "Error"
    End If
   
    With Application
        .ScreenUpdating = True: .EnableEvents = True
    End With
End Sub

Dave
Works great!!! Thank you very much!!
 
Upvote 0
Although the code above runs perfectly I need something more. Can you please advice on how to change it so I can select the range through a message box? Thank you in advance for any help provided.
 
Upvote 0
Hi.. I am curious if this code is able to be modified to select a date range? I have a workbook with tabs dated that need to be copied and paste into a different sheet.
Having a date range would work perfect so that each week, we only select new dates as applicable to the week we need.
Any help would be greatly appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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