Selectively make a sheet in a new workbook hidden

Rickinnocal

New Member
Joined
Dec 14, 2010
Messages
31
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?
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,570
Office Version
  1. 365
Platform
  1. Windows
At that point in the code you only have one sheet in the new workbook, which is why you can't hide it.
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,570
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,613
Messages
5,625,848
Members
416,139
Latest member
MattBoard

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
Top