run 2 macro's in sequence

22868

New Member
Joined
Apr 4, 2002
Messages
13
Hi all,

Still a novice at VBA....................... Pffff hard work.

Can anybody tell me whats wrong with the following code:

Private Sub Workbook_open()

Dim TheNumber As Range

Set TheNumber = Sheets("sheet 1").Range("q3")

TheNumber.Value = TheNumber.Value + 1
[q4] = Date

End Sub

Private Sub Worksheet_open()

Sub ReNameFile()

ActiveWorkbook.SaveAs FileName:=Range("AA2").Value

End Sub


First bit works great. I am able to generate a sequential number on a sheet and add the date. Then I would like the sheet to change name (e.g. book1.xls) to the name which is in cell AA2. This only works when I manually execute the macro and I would like this to execute automaticly.

Any suggestions out there for this rookie?

Thanks,

Pete
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Hello Pete, you can only have one Open event, no duplicates are allowed. This goes the same for every module in a workbook (except Standard & Class modules). Just combine the two ..

Code:
Private Sub Workbook_open()

Dim TheNumber As Range

Set TheNumber = Sheets("sheet 1").Range("q3")

TheNumber.Value = TheNumber.Value + 1
Sheets("sheet 1").Range("q4") = Date

Me.SaveAs FileName:=Range("AA2").Value

End Sub

Assumes you have a valid filename in AA2 AND it is on the current sheet which the workbook opens up to - otherwise you'll need to expressly code which sheet it's on.
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,453
22868 said:
Private Sub Worksheet_open()
There is no such event as a Worksheet_Open event.
Zack may well have answered your question, however, if you're actually trying to run the first code with the Workbook_Open event and run the second code when a specific sheet is activated then you have a Worksheet_Activate event available to you.
 

22868

New Member
Joined
Apr 4, 2002
Messages
13
Thanks Firefyter!

I am getting closer to where I want to go. Using the template with your code results in the required name change to the content of cell AA2 BUT it also saves it instantly to the std location as set up in excel.

Is there any chance the code can be modified in such a way that it renames the file but does not save it yet? I would like to be flexible in where to store the file.

Thanks again,

Pete
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web

ADVERTISEMENT

No, it's not possible. The file must be saved in order to change the name of it. You can keep it in the same location but saved with a different name, but that sounds like what you are trying to do.

One option maybe for you to browse for a folder to save the file to. Or you could always just open the SaveAs dialog box, either manually or via code.
 

22868

New Member
Joined
Apr 4, 2002
Messages
13
running 2 macro's

Thanks again Firefyter,

Major disappointment :confused: ........... OK, what next? How about I crate a button on my sheet and assign a macro to it that does the required name change and opens up the save as dialog box? Would not know how to do that though.......... This VBA stuff is not easy to get the hang of :)

any pointer on this one?

Pete
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web

ADVERTISEMENT

Yeah, there are a lot of ways to do this. You could either go the long route, or just show the default dialog box ..

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> Show_SaveAs()
    Application.Dialogs(xlDialogSaveAs).Show "abc.xls"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

In this example, the "abc.xls" is the default name that will be shown in the name textbox. You can either omit that or change it to something you desire. A lot of folks ask for that when they ask to show the SaveAs dialog box (well, that's been my experience anyway).

There are plenty of other ways to do this as well.

HTH
 

22868

New Member
Joined
Apr 4, 2002
Messages
13
Firfytr,

OK. Thanks, trying to get it to do what I want.
Can abc.xls in the code - Show "abc.xls" - be replaced by the content of a specific cell. Something like Show "content of cell a1 on sheet 1"

I am quite sure I am asking for the impossible but you never know.......

Thanks again...............

Pete
 

22868

New Member
Joined
Apr 4, 2002
Messages
13
VICTORY!!!!!

Option Explicit

Sub Show_SaveAs()
Application.Dialogs(xlDialogSaveAs).Show Range("A1").Value

End Sub

Fist time I cracked something (very simple problem, but hey I am improving) after a pointer from Firefytr.

On to more chalanging problems!
 

Watch MrExcel Video

Forum statistics

Threads
1,118,799
Messages
5,574,377
Members
412,589
Latest member
ArtBOM
Top