Consolidate worksheets all but one.

fvisions

Board Regular
Joined
Jul 29, 2008
Messages
191
Office Version
  1. 365
Platform
  1. Windows
I have this code to consolidate 7 worksheets into one and it works great. I was then asked to add a tab for instructions for the end user with a form control button for the process. When I do this the code no longer works right. How do I update the code to ignore the "Instruction" worksheet?

VBA Code:
Dim wrk As Workbook
    Dim sht As Worksheet
    Dim trg As Worksheet
    Dim rng As Range
    Dim colCount As Integer
     
    Set wrk = ActiveWorkbook
     
    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
     
     Application.ScreenUpdating = False
     Set trg = wrk.Worksheets.Add(Before:=wrk.Worksheets(wrk.Worksheets.Count))
     trg.name = "Master"
     Set sht = wrk.Worksheets(1)
     colCount = sht.Cells(1, 255).End(xlToLeft).Column
     With trg.Cells(1, 1).Resize(1, colCount)
        .Value = sht.Cells(1, 1).Resize(1, colCount).Value
        .Font.Bold = True
    End With
     For Each sht In wrk.Worksheets
        If sht.Index = wrk.Worksheets.Count Then
            Exit For
        End If
    Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
    trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    Next sht
    trg.Columns.AutoFit
    Application.ScreenUpdating = True
    
  Columns("A:A").Select
  Selection.NumberFormat = "0000"
  Columns("B:B").Select
  Selection.NumberFormat = "0"
  Columns("D:D").Select
  Selection.NumberFormat = "mm/dd/yyyy"
  Columns("F:F").Select
  Selection.NumberFormat = "mm/dd/yyyy"
 Range("A1").Select
  Worksheets("MASTER").Cells.EntireColumn.AutoFit
    Worksheets("MASTER").Move Before:=Worksheets("9459")
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about
VBA Code:
For Each sht In wrk.Worksheets
   If LCase(sht.Name) <> "instruction" Then
      Set Rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
      trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(Rng.Rows.Count, Rng.Columns.Count).Value = Rng.Value
   End If
Next sht
 
Upvote 0
Modify loop as shown below:

Rich (BB code):
For Each sht In wrk.Worksheets
    If sh.Name <> "Instruction" Then        
        Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
        trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    End If
Next sht
 
Upvote 0
How about
VBA Code:
For Each sht In wrk.Worksheets
   If LCase(sht.Name) <> "instruction" Then
      Set Rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
      trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(Rng.Rows.Count, Rng.Columns.Count).Value = Rng.Value
   End If
Next sht
Question, does this replace code or do I add this? I still need the "Master" worksheet.
 
Upvote 0
It replaces this part of your code
VBA Code:
     For Each sht In wrk.Worksheets
        If sht.Index = wrk.Worksheets.Count Then
            Exit For
        End If
    Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
    trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    Next sht
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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