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
```
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
BenGee thank you for the quick reply. It still throws me a mistake: " End if without block it"
Where do I put "End if"?
SB
 
Upvote 0
[CODE = vba]
Ak ws.Name <> "Údaje" A ws.Name <> "Celkom", potom
[/ KÓD]
BenGee ďakujem za rýchlu odpoveď. Stále ma hádže: „Koniec ak bez blokovania“
Kam mám dať „Koniec ak“?
SB
 
Upvote 0
Try
VBA Code:
Function sheetExists(sheetToFind As String) As Boolean
 Dim ws As Worksheet
    sheetExists = False
For Each ws In Worksheets
 If ws.Name <> "Data" And ws.Name <> "Total" Then
    If sheetToFind = ws.Name Then
           sheetExists = True
           Exit Function
    End If
 End If
Next ws
End Function
 
Upvote 0
Try
VBA Code:
Function sheetExists(sheetToFind As String) As Boolean
Dim ws As Worksheet
    sheetExists = False
For Each ws In Worksheets
If ws.Name <> "Data" And ws.Name <> "Total" Then
    If sheetToFind = ws.Name Then
           sheetExists = True
           Exit Function
    End If
End If
Next ws
End Function
Michael M, Thanks,
The code looks to run in order, but the data from these sheets is added to the bottom of the table.
I need to exclude them.
Where can the hook be?
SB
 
Upvote 0
Maybe here UNTESTED
Rich (BB code):
For jCt = 2 To Sheets.Count ' From Sheet 2 to the last
    If Sheets(jCt).Name <> "Data" And Sheets(jCt).Name <> "Total" 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
 
Upvote 0
Maybe here UNTESTED
Rich (BB code):
For jCt = 2 To Sheets.Count ' From Sheet 2 to the last
    If Sheets(jCt).Name <> "Data" And Sheets(jCt).Name <> "Total" 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
This worked, but one new blank sheet was inserted between the workbooks to be copied and the workbooks not to be copied :)
 
Upvote 0
What was the new blank sheet named ?
 
Upvote 0
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"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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