inserting exclusion list to merge sheets macro

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,056
Office Version
  1. 365
Platform
  1. Windows
hi all,

I am wanting to add a bit into the merge sheets macro I use but it throws a Next without For error. The extra few lines (below in bold) seek to not merge the sheets in the workbook where there is a match to the sheet names held in a named range.

Rich (BB code):
Sub CopyFromWorksheets()

    '/// This code assumes that ALL worksheets have the same field structure; same column headings, and the same column order.
    '/// The code copies all rows into one new worksheet called Master.
    '/// think this was written by smozgur (VBA Express???)
 
    'Dim wrk As Workbook                          'Workbook object - Always good to work with object variables
    Dim sht As Worksheet                         'Object for handling worksheets in loop
    Dim trg As Worksheet                         'Master Worksheet
    Dim rng As Range                             'Range object
    Dim colCount As Integer                      'Column count in tables in the worksheets
    Dim i As Long                                'count of sheet names to exclude
    Dim ExcludeSheets As Variant                   'array of sheets to not import
     Dim z as long
    z=1

    Set wrk = ActiveWorkbook                     'Working in active workbook
     
    For Each sht In wrk.Worksheets
        If sht.Name = "Master" Then
            MsgBox "There is a worksheet called as 'Master'." & vbCrLf & _
                   "Please remove or rename this worksheet since 'Master' would be" & _
                   "the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
            Exit Sub
        End If
    Next sht
     
    'We don't want screen updating
    Application.ScreenUpdating = False
     
    'Add new worksheet as the last worksheet
    Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
    'Rename the new worksheet
    trg.Name = "Master"
    'Get column headers from the first worksheet
    'Column count first
    Set sht = wrk.Worksheets(1)
    colCount = sht.Cells(1, 255).End(xlToLeft).Column
    'Now retrieve headers, no copy&paste needed
    With trg.Cells(1, 1).Resize(1, colCount)
        .Value = sht.Cells(1, 1).Resize(1, colCount).Value
        'Set font as bold
        .Font.Bold = True
    End With
     
    'We can start loop
    For Each sht In wrk.Worksheets


    
        ExcludeSheets = Sourcewb.Worksheets("Codes").Range("NoImport")
            'For i = LBound(ExcludeSheets, 1) To UBound(ExcludeSheets, 1)
            'Debug.Print ExcludeSheets(i, 1)
            'Next i

            x = Application.Match(sht.Name, Excludedsheets, 0)
            If IsError(x) Then
            z = z + 1
    


        'If worksheet in loop is the last one, stop execution (it is Master worksheet)
        If sht.Index = wrk.Worksheets.Count Then
            Exit For
        End If
        'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
        Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
        'Put data into the Master worksheet
        trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
        
    Next sht
    
    'Fit the columns in Master worksheet
    trg.Columns.AutoFit
    
    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
     
    'Screen updating should be activated
    With Application
       
        .ScreenUpdating = True
    End With
    '///1048576 rows for excel 2007 onwards
    
End Sub

without the extra bit it merges all the sheets as it should.

this is the bothersome piece below (extracted from above).

Rich (BB code):
 'We can start loop
    For Each sht In wrk.Worksheets


    
        ExcludeSheets = Sourcewb.Worksheets("Codes").Range("NoImport")
            'For i = LBound(ExcludeSheets, 1) To UBound(ExcludeSheets, 1)
            'Debug.Print ExcludeSheets(i, 1)
            'Next i

            x = Application.Match(sht.Name, Excludedsheets, 0)
            If IsError(x) Then
            z = z + 1
    


        'If worksheet in loop is the last one, stop execution (it is Master worksheet)
        If sht.Index = wrk.Worksheets.Count Then
            Exit For
        End If
        'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
        Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
        'Put data into the Master worksheet
        trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
        
    Next sht
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try changing this
If IsError(x) Then
z = z + 1

to
If IsError(x) Then z = z + 1

or to
If IsError(x) Then
z = z + 1
End if


M.
 
  • Like
Reactions: ajm
Upvote 0
Try changing this
If IsError(x) Then
z = z + 1

to
If IsError(x) Then z = z + 1

or to
If IsError(x) Then
z = z + 1
End if


M.

Thanks Marcelo,

I used your second suggestion with a twist:

Code:
    'We can start loop    For Each sht In wrk.Worksheets
    
        ExcludedSheets = Sourcewb.Worksheets("Codes").Range("NoImport")
            'For i = LBound(ExcludedSheets, 1) To UBound(ExcludedSheets, 1)
            'Debug.Print ExcludedSheets(i, 1)
            'Next i


            x = Application.Match(sht.Name, ExcludedSheets, 0)
            If Not IsError(x) Then GoTo shtExcluded
       
'If worksheet in loop is the last one, stop execution (it is Master worksheet)
        If sht.Index = wrk.Worksheets.Count Then
            Exit For
        End If
        'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
        Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
        'Put data into the Master worksheet
        trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
shtExcluded:
z = z + 1
    Next sht
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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