Adding a worksheet

andwelch

New Member
Joined
Jun 23, 2005
Messages
20
Having trouble adding a new worksheet in excel '10. Old code use to work. keep getting error 13, type mismatch. Program is to create a blank sheet1 and hide all other sheets.

Sub Hide_Sheets()
Dim ws As Worksheet

'Statement steps through each worksheet making them invisible
'leaving sheet1 visible.

Set newsheet = Worksheets.Add
newsheet.Name = "Sheet1"

For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Sheet1" Then
ws.Visible = True
Else: ws.Visible = xlSheetVeryHidden
End If
Next ws

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try changing:

Code:
[COLOR=#0000ff]Dim[/COLOR][COLOR=#333333] ws [/COLOR][COLOR=#0000ff]As Worksheet[/COLOR][COLOR=#333333] 
[/COLOR]
Code:
[COLOR=#0000ff]Dim[/COLOR] newsheet[COLOR=#0000ff] As Worksheet[/COLOR]

Not sure if this is correct. I thought I have read something along these lines (Someone Please Correct Me If I Am Wrong)....

The data type for newsheet has not been defined. If you do not define a data type for a variable I believe the default data Type is Variant.
You are trying to Set newsheet = Worksheet.Add. However, you only Set Objects. If newsheet is assumed a Variant then it can't be Set???

ws does not need to be declared as a worksheet because it is an object in a larger collection (worksheets)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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