automatic macros

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a macro in one of my sheets of a 9 sheet file (run by ctrl z). Is there anyway to have it run automatically when I first open the file. If so, how?

Thanks
 
Put the codes in "ThisWorkbook" module in VB Editor as shown below:

Private Sub Workbook_Open()
'-------------Code Contents----------------
End Sub
 
Instead of rewritng the whole code there, cannt I somehow just write to run this macro (ctrl z)? If yes, what exactly do I write?
Thanks
 
Hi
sorry. In the first suggestion just use
Public sub workbook_open()
your_macro_name
end sub
 
Where and how am I putting this?

Frank Kabel said:
Hi
sorry. In the first suggestion just use
Public sub workbook_open()
your_macro_name
end sub
 
Hi
as said by Anson: In your workbook module ('ThisWorkbook') in the VBA
editor
 
-----Original Message-----
I have a macro in one of my sheets of a 9 sheet file (run
by ctrl z). Is there anyway to have it run automatically
when I first open the file. If so, how?

itsme,

Select Tools->Macro->Visual Basic Editor from the menu
bar. By default it will open with the "Microsoft Excel
Objects" folder opened to "Sheet1 (Sheet1)" double-click
on "ThisWorkbook". Now copy/paste the following into the
whitespace that opened up when you double-
clicked "ThisWorkbook". Save and reopen your file. Upon
opening your file again, the words "Hello World" will
appear in the R1C1(A1 column/row) of all nine of your
worksheets. Ultimately you may want to purchase a book on
VBA(Visual Basic for Applications) to help you out. Check
out the MSDN page of Microsoft too. There is a ton of
help there.

Kevin K.

Private Sub Workbook_Open()
Range("A1").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet2").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet3").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet4").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet5").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet6").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet7").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet8").Select
ActiveCell.FormulaR1C1 = "Hello World"
Sheets("Sheet9").Select
ActiveCell.FormulaR1C1 = "Hello World"
Range("A2").Select
End Sub
 
Hi
try the following to get to your workbook module (this module stores
the workbook event code procedures):
- open your Excel file
- open the VBA editor (hit ALT+F11)
- in the left explorer window should be an item named 'ThisWorkbook'
- double click on this entry
- paste the code in the appearing editor window

To learn more about event procedures take a look at:
http://www.cpearson.com/excel/events.htm
 
I have nothing that says 'ThisWorkbook'?
What do you mean by the left explorer window?

Thanks for all your help!
 
Hi
what do you see if you open the VBA editor (maybe you have to hit
CTRL+R to open the treeview)
 
In the VBA Editor, press CTRL+R to open the Project Explorer
window. In this window, you should see a tree-view with a
"folder" icon for each open workbook. Click the Plus sign to
expand your workbook. There, you should see a "folder" called
"Microsoft Excel Objects". Click the Plus sign to expand that,
and you'll see a "file" icon called ThisWorkbook. Double-click
that to open the ThisWorkbook code module.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Seems to me that everything has been stated a few times.

Project Library is another name for the workbooks, so if your
workbook is named myworkbook.xls bring up the VBE
then Ctrl+R (View, Project Explorer) then for your t project
name and expand everything, you will see ThisWorkBook after
a directory of sheetnames.

A shortcut for getting to ThisWorkBook for installing
Workbook Events is:
right-click on the Excel logo to left of file menu, view code

BTW, when you find it what part was the key that wasn't
stated before.
 
Thanks everone, it finally works!!!!
Great

If I want to have more then one macro run when the file opens do I just add more at the same spot?

Example:

Public sub workbook_open()
Macro18
end sub

Public sub workbook_open()
Macro41
end sub
 
HELP!

I tried what I questioned in my last email and it doesn't work for more then one macro!
What am I doing wrong?

THANKS
 
Just do it like this:

Public sub workbook_open()

call Macro18
call Macro41

end su
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top