Printing tabs based on cell values on a seperate tab

cgwashb

New Member
Joined
Dec 13, 2014
Messages
9
I am new to VBA and to the forum, sorry if I am posting this in the wrong location.

I am working on a macro and have it nearly complete, I have created a tab that will tell the user if they should print or delete the tab (based on whether or not there is information on the sheet or not) so that they dont have to select each tab individually. I would like to take this one step further and just have the unused tabs deleted automatically based on the same concept.

The cell on the print sheet would either say print or delete, or it could be 1 or 0 if that would make it easier... The macro would reference the cell and say for "tab 1" if the specified cell says print, it will print, if it says delete, the tab will be deleted.

This would help a lot so that the user doesn't waste ink and paper, the range of pages could vary from 1-13 and the process will need to be done 100's of times to a few pages for each will save a lot of trees lol.

Thanks for you help! I hope I have explained this well enough :)
 
Welcome to the forum, and to VBA!

This is easily done. In essence, to delete a sheet all you need is:

Code:
Application.DisplayAlerts = False 'unless you want the user to confirm the delete?
Sheets("SomeSheetName").delete
Application.DisplayAlerts = True

But in more detail, perhaps you'll need something like:

Excel 2010
AB
SheetNameDelete?
Sheet2
Sheet3
Sheet4
Sheet5

<tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"]TRUE[/TD]

[TD="align: center"]3[/TD]

[TD="align: right"]FALSE[/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]TRUE[/TD]

[TD="align: center"]5[/TD]

[TD="align: right"]TRUE[/TD]

</tbody>
Sheet1

ControlRange: A2:B5

Code:
Sub DeleteSheets()

    Dim vOld As Variant, vNew As Variant
    Dim wsControlRange As Worksheet
    Dim i As Long, lCount As Long
    
    With Range("ControlRange")
        Set wsControlRange = .Parent
        vOld = .Value
        .ClearContents
    End With
    vNew = vOld
    Application.DisplayAlerts = False
    
    For i = 1 To UBound(vOld)
        If vOld(i, 2) And vOld(i, 1) <> wsControlRange.Name Then 'Don't delete the control sheet!
            On Error Resume Next 'e.g. sheet doesn't exist
            Sheets(vOld(i, 1)).Delete
            On Error GoTo 0
        Else
            lCount = lCount + 1
            vNew(lCount, 1) = vOld(i, 1)
            vNew(lCount, 2) = vOld(i, 2)
        End If
    Next i
    
    With Range("A2").Resize(lCount, 2)
        .Value = vNew
        .Name = "ControlRange"
    End With
    Application.DisplayAlerts = True

End Sub

Feel free to post your code if you need help customising it.
 
Upvote 0

Forum statistics

Threads
1,226,835
Messages
6,193,230
Members
453,781
Latest member
Buzby

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