Rookie VBA question - With

Alriemer

Board Regular
Joined
Aug 18, 2002
Messages
102
Here is a rookie VBA question stemming from 3d's post about opening a workbook template with number of sheets at user discretion. Jimboy's code works great:

http://216.92.17.166/board/viewtopic.php?topic=27921&forum=2

However I cannot leave well enough alone. I would like to hide the original sheet so that the user always has a blank template if necessary. The problem is, rather than hiding Sheet1, my code either hides all the other sheets or gives runtime error 424. Due to inexperience with WITH.

Can anyone spot issues in code below - much obliged for improving my understanding.

Alriemer

Sub auto_open()
'
Application.ScreenUpdating = False
Dim no_of_sheets
Dim counter As Integer
counter = 0
no_of_sheets = InputBox("How many sheets do you need?", "No. of sheets")
Do Until no_of_sheets = counter

Sheets("Sheet1").Copy Before:=Sheets(1)
counter = counter + 1
Loop
With Worksheets("Sheet1")
.Visible = xlSheetHidden
End With
Application.ScreenUpdating = True
Sheet2.Select
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
I can't find anything wrong with your code, except that if no_of_sheets is zero you can't hide Sheet1 (Error 1004).
 

Alriemer

Board Regular
Joined
Aug 18, 2002
Messages
102
Andrew,

I haven't restricted No_of_Sheets but should always be at least 1: you'll get one copy plus the original.

Testing again, the macro does what I want except for selecting Sheet2 (the first copy) at the end. Not that it matters which copy is selected but I'd like to avoid runtime error.

Thanks,
Alriemer
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Sorry, yes I got that error, commented it out and forgot about it.

Try:

Sheets(2).Activate
 

Forum statistics

Threads
1,143,613
Messages
5,719,713
Members
422,242
Latest member
hishamkhatri

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