Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Running code when a worksheet opens

I have built some code asking the user how many worksheets he would like to
add to a workbook with 1 visible worksheet. It is attached to a button and I
now need to put that code in a property such as "On Open" (I'm better with
Access), so when the worksheet opens, the first question is "How many...".
I've read some of the other posts, but can't find the property/ies they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden worksheets
(used for data validation). If not "Private Sub Worksheet_Open()", then
where?

Thanks in advance for any help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Running code when a worksheet opens

go into the vbe and in the project explorer pick you project. Under you
project click on the ThisWorkbook entry and select view code. Place your
code there.

--
Regards,
Tom Ogilvy


"ReportSmith" wrote:

I have built some code asking the user how many worksheets he would like to
add to a workbook with 1 visible worksheet. It is attached to a button and I
now need to put that code in a property such as "On Open" (I'm better with
Access), so when the worksheet opens, the first question is "How many...".
I've read some of the other posts, but can't find the property/ies they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden worksheets
(used for data validation). If not "Private Sub Worksheet_Open()", then
where?

Thanks in advance for any help.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Running code when a worksheet opens

Thanks for the tip. I copied the code into the project section, but the file
won't open correctly...it locks up after it opens and I can't do anything.

"Tom Ogilvy" wrote:

go into the vbe and in the project explorer pick you project. Under you
project click on the ThisWorkbook entry and select view code. Place your
code there.

--
Regards,
Tom Ogilvy


"ReportSmith" wrote:

I have built some code asking the user how many worksheets he would like to
add to a workbook with 1 visible worksheet. It is attached to a button and I
now need to put that code in a property such as "On Open" (I'm better with
Access), so when the worksheet opens, the first question is "How many...".
I've read some of the other posts, but can't find the property/ies they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden worksheets
(used for data validation). If not "Private Sub Worksheet_Open()", then
where?

Thanks in advance for any help.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Running code when a worksheet opens

Sorry...I got it. I had accidentally placed the code in the General
Declarations section. But the locking up issue still exists after all of the
sheets have been copied. In other words, if I enter "9", 9 extra sheets
appear and then I'm locked up and can't work within any of the sheets.

"Tom Ogilvy" wrote:

go into the vbe and in the project explorer pick you project. Under you
project click on the ThisWorkbook entry and select view code. Place your
code there.

--
Regards,
Tom Ogilvy


"ReportSmith" wrote:

I have built some code asking the user how many worksheets he would like to
add to a workbook with 1 visible worksheet. It is attached to a button and I
now need to put that code in a property such as "On Open" (I'm better with
Access), so when the worksheet opens, the first question is "How many...".
I've read some of the other posts, but can't find the property/ies they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden worksheets
(used for data validation). If not "Private Sub Worksheet_Open()", then
where?

Thanks in advance for any help.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Running code when a worksheet opens

Test it in a new workbook (with two hidden sheets) and a blank sheet named "1"

If it works there, then it must have something to do with the data on the
sheet being copied or something to do with recalculating.

--
Regards,
Tom Ogivly



"ReportSmith" wrote:

Sorry...I got it. I had accidentally placed the code in the General
Declarations section. But the locking up issue still exists after all of the
sheets have been copied. In other words, if I enter "9", 9 extra sheets
appear and then I'm locked up and can't work within any of the sheets.

"Tom Ogilvy" wrote:

go into the vbe and in the project explorer pick you project. Under you
project click on the ThisWorkbook entry and select view code. Place your
code there.

--
Regards,
Tom Ogilvy


"ReportSmith" wrote:

I have built some code asking the user how many worksheets he would like to
add to a workbook with 1 visible worksheet. It is attached to a button and I
now need to put that code in a property such as "On Open" (I'm better with
Access), so when the worksheet opens, the first question is "How many...".
I've read some of the other posts, but can't find the property/ies they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden worksheets
(used for data validation). If not "Private Sub Worksheet_Open()", then
where?

Thanks in advance for any help.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Running code when a worksheet opens

I have a feeling you're right. The validation rules look up values on the 2
hidden sheets. The "locking" goes away after a bit (5-10 seconds?) and I am
able to work with the sheets. This makes me think that some background
calculation is taking place (the validation lookups - there are 2 of them
with one depending on the other).
Is there any way to speed this up other than copying the sheet and pasting
it into another workbook as values, without the validation lookup(s)?

Thanks again.

"Tom Ogilvy" wrote:

Test it in a new workbook (with two hidden sheets) and a blank sheet named "1"

If it works there, then it must have something to do with the data on the
sheet being copied or something to do with recalculating.

--
Regards,
Tom Ogivly



"ReportSmith" wrote:

Sorry...I got it. I had accidentally placed the code in the General
Declarations section. But the locking up issue still exists after all of the
sheets have been copied. In other words, if I enter "9", 9 extra sheets
appear and then I'm locked up and can't work within any of the sheets.

"Tom Ogilvy" wrote:

go into the vbe and in the project explorer pick you project. Under you
project click on the ThisWorkbook entry and select view code. Place your
code there.

--
Regards,
Tom Ogilvy


"ReportSmith" wrote:

I have built some code asking the user how many worksheets he would like to
add to a workbook with 1 visible worksheet. It is attached to a button and I
now need to put that code in a property such as "On Open" (I'm better with
Access), so when the worksheet opens, the first question is "How many...".
I've read some of the other posts, but can't find the property/ies they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden worksheets
(used for data validation). If not "Private Sub Worksheet_Open()", then
where?

Thanks in advance for any help.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Running code when a worksheet opens

application.Calculation = xlManual
Application.EnableEvents = False
Application.ScreenUpdating = False

' code to copy the sheets

Application.EnableEvents = True
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

--
Regards,
Tom Ogilvy


"ReportSmith" wrote in message
...
I have a feeling you're right. The validation rules look up values on the
2
hidden sheets. The "locking" goes away after a bit (5-10 seconds?) and I
am
able to work with the sheets. This makes me think that some background
calculation is taking place (the validation lookups - there are 2 of them
with one depending on the other).
Is there any way to speed this up other than copying the sheet and pasting
it into another workbook as values, without the validation lookup(s)?

Thanks again.

"Tom Ogilvy" wrote:

Test it in a new workbook (with two hidden sheets) and a blank sheet
named "1"

If it works there, then it must have something to do with the data on the
sheet being copied or something to do with recalculating.

--
Regards,
Tom Ogivly



"ReportSmith" wrote:

Sorry...I got it. I had accidentally placed the code in the General
Declarations section. But the locking up issue still exists after all
of the
sheets have been copied. In other words, if I enter "9", 9 extra
sheets
appear and then I'm locked up and can't work within any of the sheets.

"Tom Ogilvy" wrote:

go into the vbe and in the project explorer pick you project. Under
you
project click on the ThisWorkbook entry and select view code. Place
your
code there.

--
Regards,
Tom Ogilvy


"ReportSmith" wrote:

I have built some code asking the user how many worksheets he would
like to
add to a workbook with 1 visible worksheet. It is attached to a
button and I
now need to put that code in a property such as "On Open" (I'm
better with
Access), so when the worksheet opens, the first question is "How
many...".
I've read some of the other posts, but can't find the property/ies
they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden
worksheets
(used for data validation). If not "Private Sub Worksheet_Open()",
then
where?

Thanks in advance for any help.



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Running code when a worksheet opens

Should I place this code in the Workbook Open property sheet? The
workbook hangs only when it opens. When I add a sheet, everything seems ok.
If so, the calculation will only take place if the user presses <Shift+F9 -
right?

"Tom Ogilvy" wrote:

application.Calculation = xlManual
Application.EnableEvents = False
Application.ScreenUpdating = False

' code to copy the sheets

Application.EnableEvents = True
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

--
Regards,
Tom Ogilvy


"ReportSmith" wrote in message
...
I have a feeling you're right. The validation rules look up values on the
2
hidden sheets. The "locking" goes away after a bit (5-10 seconds?) and I
am
able to work with the sheets. This makes me think that some background
calculation is taking place (the validation lookups - there are 2 of them
with one depending on the other).
Is there any way to speed this up other than copying the sheet and pasting
it into another workbook as values, without the validation lookup(s)?

Thanks again.

"Tom Ogilvy" wrote:

Test it in a new workbook (with two hidden sheets) and a blank sheet
named "1"

If it works there, then it must have something to do with the data on the
sheet being copied or something to do with recalculating.

--
Regards,
Tom Ogivly



"ReportSmith" wrote:

Sorry...I got it. I had accidentally placed the code in the General
Declarations section. But the locking up issue still exists after all
of the
sheets have been copied. In other words, if I enter "9", 9 extra
sheets
appear and then I'm locked up and can't work within any of the sheets.

"Tom Ogilvy" wrote:

go into the vbe and in the project explorer pick you project. Under
you
project click on the ThisWorkbook entry and select view code. Place
your
code there.

--
Regards,
Tom Ogilvy


"ReportSmith" wrote:

I have built some code asking the user how many worksheets he would
like to
add to a workbook with 1 visible worksheet. It is attached to a
button and I
now need to put that code in a property such as "On Open" (I'm
better with
Access), so when the worksheet opens, the first question is "How
many...".
I've read some of the other posts, but can't find the property/ies
they refer
to.

So far, I have:
------------------------------------
Private Sub Worksheet_Open()
Dim NumSheets, x, y

y = Worksheets.Count
NumSheets = InputBox("How many sheets to add?")
If NumSheets = Empty Then
End
Else
For x = 1 To NumSheets
Sheets("1").Select
Sheets("1").Copy After:=Sheets(1)
y = y + 1
ActiveSheet.Name = y - 2
Next x
End If
End Sub
------------------------------------

The reason I have "y - 2" above is because I also have 2 hidden
worksheets
(used for data validation). If not "Private Sub Worksheet_Open()",
then
where?

Thanks in advance for any help.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running Code on Hidden Worksheet Steve C Excel Programming 2 February 13th 07 06:52 PM
Refer to the worksheet containing the current running code [email protected] Excel Programming 2 November 30th 06 10:14 AM
how to prevent code running when in a worksheet code Corey Excel Programming 5 August 13th 06 08:52 AM
Running VBA Code not written within the currrent worksheet DMB Excel Programming 2 September 6th 05 05:58 PM
Running Code in an XLA from a Worksheet Chrissy[_4_] Excel Programming 6 October 11th 03 11:27 PM


All times are GMT +1. The time now is 10:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"