Selectively make a sheet in a new workbook hidden

Rickinnocal

New Member
Joined
Dec 14, 2010
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hello again...

I have a command button that creates a new workbook with a number of worksheets in it, populated from the "Parent" workbook.

Depending on the job, some of the sheets will be blank, and can be hidden. At the moment the workbook opens with all sheets visible. I'm trying to code in the decision as to whether to hide a sheet or not.

This is how I'm trying it...

VBA Code:
Private Sub cmdCMAeFile_Click()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

    'Path to store new file
    sPath = "C:\"
    sFileName = InputBox("Enter file name for new BQS eFile. Recommend Ship Name / Port / BQS eFile")
    oFileName = ActiveWorkbook.Name
    
    'set the sheet to be copied
    Set wsCopy = ThisWorkbook.Worksheets("Prelim (FO)")
    Set wb = Workbooks.Add
    wb.SaveAs Filename:=(sFileName)
    Set wsPaste = wb.Sheets(1)
    
 'Copy everything from copy sheet
    wsCopy.UsedRange.Copy
    'Paste Values and Formats only
    wsPaste.Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
    wsPaste.Range("A1").PasteSpecial xlPasteFormats
    wsPaste.Range("A1").PasteSpecial xlPasteColumnWidths
    ActiveSheet.Name = "Prelim (FO)"
    If Cells(26, 2).Value = 0 Then
    ActiveSheet.Visible = xlSheetHidden
    End If
    ActiveSheet.Protect

The code then repeats itself several times to add the rest of the sheets. However, when run it stops at the line
VBA Code:
ActiveSheet.Visible = xlSheetHidden
with an error "Unable to set the Visible property of the Worksheet class".

If I put something in the target cell so that the If / Then is false, it skips the line and proceeds as expected.

What am I doing wrong?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
At that point in the code you only have one sheet in the new workbook, which is why you can't hide it.
 
Upvote 0
Solution
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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