How omit sheets from the loop?

Slavio

Board Regular
Joined
Mar 28, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
  2. Web
I have been following this forum for several weeks and I have found many answers here, for which I thank you very much! I would also like to contribute in the future.
But now he has this question that I can't handle.
How omit sheets from the loop?

I want to skip copying tables from sheets named "Data" and "Total".

Code below. It goes through all the sheets and adds data to a new sheet called "Together".
However, I want him to omit two specific sheets.

I guess it'll be something like
VBA Code:
`If Not Sheets(jCt).Name = "Data" and "Total" Then`
But I don't know how to finish it

How do I do that?
Any help will be welcome
```
VBA Code:
 Sub Combination()
Dim jCt As Integer
Dim ws As Worksheets
Dim myRange As Range
Dim lastRow As Long
lastRow = 1

'Removes the "Together" sheet, if any
If sheetExists("Together") Then
    Application.DisplayAlerts = False
    Sheets("Together").Delete
    Application.DisplayAlerts = True
    MsgBox "Worksheet ""Together"" deleted!"
End If

Worksheets.Add ' Adds a sheet to the first place
Sheets(1).Name = "Together"

' Sheet processing
For jCt = 2 To Sheets.Count ' From Sheet 2 to the last

    Set myRange = Sheets(jCt).Range(Sheets(jCt).Cells(1, 1), Sheets(jCt).Range("A1").SpecialCells(xlCellTypeLastCell))
    Debug.Print Sheets(jCt).Name, myRange.Address

    'Copying Sheets
    myRange.Copy Destination:=Sheets("Together").Range("A1").Offset(lastRow - 1, 0)
    lastRow = lastRow + myRange.Rows.Count + 0 ' Adds the number of rows below the last record

Next

MsgBox "The sheet ""Together"" is created"
End Sub
Function sheetExists(sheetToFind As String) As Boolean
    sheetExists = False
    For Each Sheet In Worksheets
        If sheetToFind = Sheet.Name Then
            sheetExists = True
            Exit Function
        End If
    Next Sheet
End Function
```
 
What was the new blank sheet named ?
He didn't name it. But the last sheet was renamed, the data was erased, and the new name is Sheet5.
I'll try to clarify once again what I need, maybe you can think of another solution.
1. I want data from all existing sheets to be collected in a new workbook called "Total"
2. In the last one I have data that will not be copied.
3. The "Data" sheet will also not be copied
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Change these lines
VBA Code:
Worksheets.Add ' Adds a sheet to the first place
Sheets(1).Name = "Together"
To
VBA Code:
Worksheets.Add.Name = "Together"
OK, many thanks. This worked exactly as expected
Here is the function code:
VBA Code:
Sub Combination222()
Dim jCt As Integer
Dim ws As Worksheets
Dim myRange As Range
Dim lastRow As Long
lastRow = 1

'Removes the "Together" sheet, if any
If sheetExists("Together") Then
    Application.DisplayAlerts = False
    Sheets("Together").Delete
    Application.DisplayAlerts = True
    MsgBox "Worksheet ""Together"" deleted!"
End If

Worksheets.Add.Name = "Together"

' Sheet processing
For jCt = 2 To Sheets.Count ' From Sheet 2 to the last
If Sheets(jCt).Name <> "SETUP" Then

    Set myRange = Sheets(jCt).Range(Sheets(jCt).Cells(1, 1), Sheets(jCt).Range("A1").SpecialCells(xlCellTypeLastCell))
    Debug.Print Sheets(jCt).Name, myRange.Address

    'Copying Sheets
    myRange.Copy Destination:=Sheets("Together").Range("A1").Offset(lastRow - 1, 0)
    lastRow = lastRow + myRange.Rows.Count + 0 ' Adds the number of rows below the last record

End If
Next
MsgBox "The sheet ""Together"" is created"
End Sub
Function sheetExists(sheetToFind As String) As Boolean
    sheetExists = False
    For Each Sheet In Worksheets
        If sheetToFind = Sheet.Name Then
            sheetExists = True
            Exit Function
        End If
    Next Sheet
End Function

SB
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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