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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I can't find anything wrong with your code, except that if no_of_sheets is zero you can't hide Sheet1 (Error 1004).
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,869
Members
449,054
Latest member
juliecooper255

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