|
-
Jan 18th, 2008, 08:37 AM
#1
Run Code On Open Spreadsheet
I have some code that I would like to run each time a specific worksheet is opened in Excel.
What is the spreadsheet equivalent to Form_Load?
-
Jan 18th, 2008, 08:41 AM
#2
Re: Run Code On Open Spreadsheet
For worksheet it is
vb Code:
Private Sub Worksheet_Activate()
MsgBox "Hi Hack. You have selected a sheet"
End Sub
For workbook it is....
vb Code:
Private Sub Workbook_Open()
MsgBox "Hi Hack"
End Sub
Hope it helps...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2008, 08:46 AM
#3
Re: Run Code On Open Spreadsheet
Are these built in Subs or do I have to create them myself?
-
Jan 18th, 2008, 08:49 AM
#4
Re: Run Code On Open Spreadsheet
I presume you mean Workbook_Open - it's built in and you'll find it when you go into the ThisWorkbook object from the Project window.
Worksheet_Activate will fire whenever you select a particular worksheet in a book.
-
Jan 18th, 2008, 08:49 AM
#5
Re: Run Code On Open Spreadsheet
They are built in.
In an Excel sheet press Alt=F11 to open the VBA Editor then double click the relevant Sheet to open the code window.
In 'General' select the sheet and in "declaration" select the event 
Hope this helps...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2008, 08:56 AM
#6
Re: Run Code On Open Spreadsheet
Cool!
That code that you posted on clearing out the textboxs got copied to Worksheet_Activate so it does it when the spreadsheet is opened.
Now all I have to do is figure out a way to force them to do a SaveAs rather than a Save (which continously overwrites the template spreadsheet that I've been asked to do some things with), life will become a little easier.
-
Jan 18th, 2008, 09:15 AM
#7
Re: Run Code On Open Spreadsheet
 Originally Posted by Hack
Cool!
That code that you posted on clearing out the textboxs got copied to Worksheet_Activate so it does it when the spreadsheet is opened.
Now all I have to do is figure out a way to force them to do a SaveAs rather than a Save (which continously overwrites the template spreadsheet that I've been asked to do some things with), life will become a little easier.
Hi there are two ways of doing it...
1) Without Code.
When you save the intial file, in the "save/save as", under the "Tool options" click on "General options". In the "Save options", type the password for "Modify" and put a "check" on the box which says "Readonly". This will make sure that the users will always have to save a copy.
2) With Code (Try recording a macro for the above steps)
Replace "P:\temp.xls" in the below code with the respective filename "abcde" with a password of your choice...
vb Code:
Sub Macro1()
ActiveWorkbook.SaveAs Filename:="P:\temp.xls", FileFormat:=xlNormal, _
Password:="", WriteResPassword:="abcde", ReadOnlyRecommended:=True, _
CreateBackup:=False
End Sub
Hope this helps...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2008, 09:31 AM
#8
Re: Run Code On Open Spreadsheet
 Originally Posted by koolsid
Hi there are two ways of doing it...
1) Without Code.
When you save the intial file, in the "save/save as", under the "Tool options" click on "General options". In the "Save options", type the password for "Modify" and put a "check" on the box which says "Readonly". This will make sure that the users will always have to save a copy.
I don't understand this one. I can't find the Options you are referring to.
-
Jan 18th, 2008, 09:34 AM
#9
Re: Run Code On Open Spreadsheet
It comes when you click on Menu File=>SaveAs
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2008, 09:43 AM
#10
Re: Run Code On Open Spreadsheet
But, that doesn't stop them from doing a straight "Save".
-
Jan 18th, 2008, 09:52 AM
#11
Re: Run Code On Open Spreadsheet
Can the readonly property be set via code? If everytime the workbook opens it marked itslf as readonly, surely that would be enough to cause Excel to do a SAve As even if they hit the save button.
-tg
-
Jan 18th, 2008, 10:32 AM
#12
Re: Run Code On Open Spreadsheet
 Originally Posted by techgnome
Can the readonly property be set via code? If everytime the workbook opens it marked itslf as readonly, surely that would be enough to cause Excel to do a SAve As even if they hit the save button.
-tg
I like this......is this possible?
-
Jan 18th, 2008, 10:43 AM
#13
Re: Run Code On Open Spreadsheet
I imagine so... I remember years ago in a former life when I had to open Word files, turn off the QuickSave option then Save As a new file.... given that that was possible, I imagine this should be as well.... Try recording a macro and then go through the steps of setting the flag.... see what it generates.
-tg
-
Jan 18th, 2008, 10:57 AM
#14
Re: Run Code On Open Spreadsheet
 Originally Posted by Hack
But, that doesn't stop them from doing a straight "Save".
yes it does
When you check the "readonly" option and save the file, you need to close it once. next time you open it, it will prompt you to open the file in "readonly" mode.... and until and unless you feed in the password it won't let you overwrite the file....
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2008, 11:01 AM
#15
Re: Run Code On Open Spreadsheet
I've gone through all of the menu options in a spreadsheet and I can't find a QuickSave option to turn off.
-
Jan 18th, 2008, 11:02 AM
#16
Re: Run Code On Open Spreadsheet
 Originally Posted by koolsid
yes it does
When you check the "readonly" option and save the file, you need to close it once. next time you open it, it will prompt you to open the file in "readonly" mode.... and until and unless you feed in the password it won't let you overwrite the file....
Ohhhh...I didn't close it first, I just hit "Save"
Let me try that.
-
Jan 18th, 2008, 11:26 AM
#17
Re: Run Code On Open Spreadsheet
 Originally Posted by Hack
I've gone through all of the menu options in a spreadsheet and I can't find a QuickSave option to turn off.
That was a WORD option from back when.... I was simply saying that if I could change that using VBA back in word95, then surely they option you're looking for exists in the same way...
-tg
-
Jan 18th, 2008, 12:32 PM
#18
Re: Run Code On Open Spreadsheet
 Originally Posted by techgnome
That was a WORD option from back when.... I was simply saying that if I could change that using VBA back in word95, then surely they option you're looking for exists in the same way...
-tg
Ah, Word. Ok, I'll keep snooping around Excel.
-
Jan 18th, 2008, 12:42 PM
#19
Re: Run Code On Open Spreadsheet
 Originally Posted by techgnome
I imagine so... I remember years ago in a former life when I had to open Word files, turn off the QuickSave option then Save As a new file.... given that that was possible, I imagine this should be as well.... Try recording a macro and then go through the steps of setting the flag.... see what it generates.
-tg
Hi tg
The Quicksave option that you are talking about i feel is (I could be wrong) is the same as "fast save" which is entirely a different concept...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2008, 01:00 PM
#20
Re: Run Code On Open Spreadsheet
Sigh.... I know that.... I was using it as an EXAMPLE..... record a macro, do the actions you want to code.... then read the macro, look at the code.... convert it to VB or stuff it into your VBA code, with the appropriate properties. MY POINT was that if I could programmatically change the FastSave (quick save, what ever) OVER 10 years ago, then it SHOULD be a simple and easy thing to do in Excel for the ReadOnly option.
Maybe I should just stay out of the Office Development forums, trouble seems to follow me...
-tg
-
Jan 18th, 2008, 02:03 PM
#21
Re: Run Code On Open Spreadsheet
Actually, this looks promising.
-
Jan 18th, 2008, 02:05 PM
#22
Re: Run Code On Open Spreadsheet
But this isn't what you wanted???
Did you try the readonly option?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 18th, 2008, 02:07 PM
#23
Re: Run Code On Open Spreadsheet
No, not yet....I got called away to "put out some fires" 
I'll give it whirl.
-
Jan 18th, 2008, 02:48 PM
#24
Re: Run Code On Open Spreadsheet
That actually seems to work fairly well.
I saved it and rolled it out the users production area.
Lets see what they think.
-
Jan 18th, 2008, 03:19 PM
#25
Re: Run Code On Open Spreadsheet
the other option is to save it as a template, then users will open a new book based on the template
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 18th, 2008, 03:22 PM
#26
Re: Run Code On Open Spreadsheet
 Originally Posted by Hack
That actually seems to work fairly well.
I saved it and rolled it out the users production area.
Lets see what they think.
That's the best way to check it
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|