inserting exclusion list to merge sheets macro

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
1,762
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
 

Some videos you may like

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
16,393
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

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
1,762
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,271
Messages
5,527,708
Members
409,784
Latest member
lalz1205

This Week's Hot Topics

Top