Upgrade the code to ignore empty sheets

An Quala

Board Regular
Joined
Mar 21, 2022
Messages
146
Office Version
  1. 2021
Platform
  1. Windows
Hello, I would like to upgrade these codes to not run if the sheets are empty. So the problem is usually 2 of these 3 sheets are empty when I run the code but it works for all 3 sheets, so I don't want it to run on empty sheets. If any of these 3 sheets are empty (Which could be found by checking the cell A2 of any sheet) then it should ignore it.

VBA Code:
Dim c As Range, va, x
     For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
        Set c = Worksheets(x).UsedRange
        va = c.Value
        Worksheets(x).Cells.NumberFormat = "General"
        Worksheets(x).Cells.Interior.Color = xlNone
        c = va
    Next

VBA Code:
For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
          s = s & Sheets(CStr(x)).Range("A" & Rows.Count).End(xlUp).Row - 1 & " "     'collect the last rows and use the space as separator
     Next
     sp = Split(s)                                              'split string on space
     MsgBox sp(0) & " SP, " & sp(1) & " SB and " & sp(2) & " SD targets have been optimized in " & Format(dTime, "0.0") & "seconds."    'make the text
     
    With ThisWorkbook.Sheets(Array("Sponsored Products Campaigns", "Sponsored Display Campaigns", "Sponsored Brands Campaigns"))
        .Copy
    End With
    
    With ActiveWorkbook
        .Worksheets("Sponsored Products Campaigns").Range("F:G,J:Q,S:Y,AA:AS").EntireColumn.Delete
        .Worksheets("Sponsored Brands Campaigns").Range("E:F,J:N,P:U,W:AY").EntireColumn.Delete
        .Worksheets("Sponsored Display Campaigns").Range("E:E,G:G,I:O,Q:Y,AA:AP").EntireColumn.Delete
    End With


I think both of the above codes might have a same solution, kindly help, thank you.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Since blank sheet will have .usedrange = first cell A1 then
VBA Code:
For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
    Set c = Worksheets(x).UsedRange
    If c.Address <> "$A$1" Then
        va = c.Value
        Worksheets(x).Cells.NumberFormat = "General"
        Worksheets(x).Cells.Interior.Color = xlNone
        c = va
    End If
Next
 
Upvote 0
Since blank sheet will have .usedrange = first cell A1 then

Thank you @bebo021999, can you please tell me the solution for the second code as well.

VBA Code:
For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
          s = s & Sheets(CStr(x)).Range("A" & Rows.Count).End(xlUp).Row - 1 & " "     'collect the last rows and use the space as separator
     Next
     sp = Split(s)                                              'split string on space
     MsgBox sp(0) & " SP, " & sp(1) & " SB and " & sp(2) & " SD targets have been optimized in " & Format(dTime, "0.0") & "seconds."    'make the text
     
    With ThisWorkbook.Sheets(Array("Sponsored Products Campaigns", "Sponsored Display Campaigns", "Sponsored Brands Campaigns"))
        .Copy
    End With
    
    With ActiveWorkbook
        .Worksheets("Sponsored Products Campaigns").Range("F:G,J:Q,S:Y,AA:AS").EntireColumn.Delete
        .Worksheets("Sponsored Brands Campaigns").Range("E:F,J:N,P:U,W:AY").EntireColumn.Delete
        .Worksheets("Sponsored Display Campaigns").Range("E:E,G:G,I:O,Q:Y,AA:AP").EntireColumn.Delete
    End With
 
Upvote 0
Same If right before main code
VBA Code:
For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
    If Worksheets(x).UsedRange.Address <> "$A$1" Then
................................< your main code.....................
end if
end sub
 
Upvote 0
Same If right before main code
Thanks and what about this part?

VBA Code:
 With ActiveWorkbook
        .Worksheets("Sponsored Products Campaigns").Range("F:G,J:Q,S:Y,AA:AS").EntireColumn.Delete
        .Worksheets("Sponsored Brands Campaigns").Range("E:F,J:N,P:U,W:AY").EntireColumn.Delete
        .Worksheets("Sponsored Display Campaigns").Range("E:E,G:G,I:O,Q:Y,AA:AP").EntireColumn.Delete
    End With
 
Upvote 0
Like this?.
VBA Code:
For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
    If Worksheets(x).UsedRange.Address <> "$A$1" Then
          Select case  x 
                Case "Sponsored Products Campaigns"
                            Worksheets(x).range("F:G,J:Q,S:Y,AA:AS").EntireColumn.Delete
                Case "Sponsored Brands Campaigns"
           end select                ....
end if
NExt
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,687
Members
452,994
Latest member
Janick

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