Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 148
Default how to get a macro to work

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default how to get a macro to work

Try renaming that module (not the procedure) to something else:

Mod_Workbook_save
???

Danny wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default how to get a macro to work

I agree with Dave Peterson, if nothing else, bad form to use reserved words
and possibly confusing the system in this case.

You may also have a problem with the location and logic of things. The
_BeforeSave routine must be in the workbook's code segment. The other
routine should be in a regular code module.

Now the logic issue: _BeforeSave is called before any save is done, so when
you have it call your other routine, _BeforeSave gets called again and sooner
or later the Cancel=True gets hit and the save is cancelled, and the workbook
is never saved.

But if you'll set a flag in your routine to tell _BeforeSave how to act when
your other routine is being used, you can get it done. You'll need a Public
boolean variable to act as the flag. Here's what I came up with:

code for the Workbook's code module:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
If justSayYes Then
Exit Sub ' continue with save normally
End If
Call myBook_Save
Cancel = True
End Sub

code for your other routine in a normal code module, note that the flag is
defined in the declarations section of that module before any sub or function
statement:

Public justSayYes As Boolean

Sub myBook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

justSayYes = True

ActiveWorkbook.SaveAs Filename:=rng.Value & _
".xls", FileFormat:=xlWorkbookNormal

End Sub


"Dave Peterson" wrote:

Try renaming that module (not the procedure) to something else:

Mod_Workbook_save
???

Danny wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default how to get a macro to work

I think I may have had wrong post chosen when I replied before, but no matter
- I realized right after posting that we need to reset that flag after it
gets used, so the _BeforeSave code needs a slight modification:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
If justSayYes Then
justSayYes = False ' reset it
Exit Sub ' continue with save normally
End If
Call myBook_Save
Cancel = True
End Sub


"Danny" wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 148
Default how to get a macro to work

Thanks, but i'm still havig the same problem, and get the Compile error -
Expected variable or proceder, not module.

And when i change the module name, it will not give me this error, but does
not save at all.

"JLatham" wrote:

I think I may have had wrong post chosen when I replied before, but no matter
- I realized right after posting that we need to reset that flag after it
gets used, so the _BeforeSave code needs a slight modification:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
If justSayYes Then
justSayYes = False ' reset it
Exit Sub ' continue with save normally
End If
Call myBook_Save
Cancel = True
End Sub


"Danny" wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default how to get a macro to work

How do you know it's not saving?

Maybe you could put a
msgbox "Saved as " & activeworkbook.fullname
at the end of whatever the name of that procedure is that does the save.



Danny wrote:

Thanks, but i'm still havig the same problem, and get the Compile error -
Expected variable or proceder, not module.

And when i change the module name, it will not give me this error, but does
not save at all.

"JLatham" wrote:

I think I may have had wrong post chosen when I replied before, but no matter
- I realized right after posting that we need to reset that flag after it
gets used, so the _BeforeSave code needs a slight modification:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
If justSayYes Then
justSayYes = False ' reset it
Exit Sub ' continue with save normally
End If
Call myBook_Save
Cancel = True
End Sub


"Danny" wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default how to get a macro to work

Since you're forcing a SaveAs you don't get a dialog box anymore - it just
does it. You should see the name of your file change in the title bar of
Excel. Try putting a different entry into M2 and then run the routine and
see if the title bar doesn't change.

I'm stymied by the compiler error. I never got that even when I had your
original code set up completely wrong here with the intent of trying to
duplicate the problem.

Here's a workbook with my code in it that appears to work for me (Excel 2003
format). Just click the link and choose SAVE to your drive and give it a
test run. Change the entry in M2 and watch what happens when you click the
Save button or even try to use File | Save As.
http://www.jlathamsite.com/uploads/S...nterrupted.xls

"Danny" wrote:

Thanks, but i'm still havig the same problem, and get the Compile error -
Expected variable or proceder, not module.

And when i change the module name, it will not give me this error, but does
not save at all.

"JLatham" wrote:

I think I may have had wrong post chosen when I replied before, but no matter
- I realized right after posting that we need to reset that flag after it
gets used, so the _BeforeSave code needs a slight modification:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
If justSayYes Then
justSayYes = False ' reset it
Exit Sub ' continue with save normally
End If
Call myBook_Save
Cancel = True
End Sub


"Danny" wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks

  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 148
Default how to get a macro to work

Thanks, The link worked i have copied the macro into my worksheet and it is
working now, just one question.

Is it correct that this macro only does it once, after changing the file
name and press save it change the filename, but when i change M2 again it
saves it over the first name.

Thanks a lot for your help

"JLatham" wrote:

Since you're forcing a SaveAs you don't get a dialog box anymore - it just
does it. You should see the name of your file change in the title bar of
Excel. Try putting a different entry into M2 and then run the routine and
see if the title bar doesn't change.

I'm stymied by the compiler error. I never got that even when I had your
original code set up completely wrong here with the intent of trying to
duplicate the problem.

Here's a workbook with my code in it that appears to work for me (Excel 2003
format). Just click the link and choose SAVE to your drive and give it a
test run. Change the entry in M2 and watch what happens when you click the
Save button or even try to use File | Save As.
http://www.jlathamsite.com/uploads/S...nterrupted.xls

"Danny" wrote:

Thanks, but i'm still havig the same problem, and get the Compile error -
Expected variable or proceder, not module.

And when i change the module name, it will not give me this error, but does
not save at all.

"JLatham" wrote:

I think I may have had wrong post chosen when I replied before, but no matter
- I realized right after posting that we need to reset that flag after it
gets used, so the _BeforeSave code needs a slight modification:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
If justSayYes Then
justSayYes = False ' reset it
Exit Sub ' continue with save normally
End If
Call myBook_Save
Cancel = True
End Sub


"Danny" wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks

  #9   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default how to get a macro to work

It just saves using the filename in M2 - haven't tested, but it should
overwrite file of the same name. Easy test:
make an entry into some cell other than M2, use it. Close the file, reopen
it to verify that the entry in that cell is as you made it. Change that
entry - reuse the function and close the file and open again to see that the
entry has also changed.

"Danny" wrote:

Thanks, The link worked i have copied the macro into my worksheet and it is
working now, just one question.

Is it correct that this macro only does it once, after changing the file
name and press save it change the filename, but when i change M2 again it
saves it over the first name.

Thanks a lot for your help

"JLatham" wrote:

Since you're forcing a SaveAs you don't get a dialog box anymore - it just
does it. You should see the name of your file change in the title bar of
Excel. Try putting a different entry into M2 and then run the routine and
see if the title bar doesn't change.

I'm stymied by the compiler error. I never got that even when I had your
original code set up completely wrong here with the intent of trying to
duplicate the problem.

Here's a workbook with my code in it that appears to work for me (Excel 2003
format). Just click the link and choose SAVE to your drive and give it a
test run. Change the entry in M2 and watch what happens when you click the
Save button or even try to use File | Save As.
http://www.jlathamsite.com/uploads/S...nterrupted.xls

"Danny" wrote:

Thanks, but i'm still havig the same problem, and get the Compile error -
Expected variable or proceder, not module.

And when i change the module name, it will not give me this error, but does
not save at all.

"JLatham" wrote:

I think I may have had wrong post chosen when I replied before, but no matter
- I realized right after posting that we need to reset that flag after it
gets used, so the _BeforeSave code needs a slight modification:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
If justSayYes Then
justSayYes = False ' reset it
Exit Sub ' continue with save normally
End If
Call myBook_Save
Cancel = True
End Sub


"Danny" wrote:

Hi There,

I have received some info through this posting, but it is not working how i
want to.
Hoppefully someone can help me further?

I have a Macro in a Module (Workbook_save)

Sub Workbook_Save()
Dim rng As Range

Set rng = ActiveSheet.Range("M2")

ActiveWorkbook.SaveAs _
Filename:=rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

End Sub

And a macro to start up the above in This workbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call Workbook_Save
Cancel = True
End Sub

But for some reason it is not saving the file as supose to do, i get the
error:
Compile Error - Expected variable or Proceder, not module

How can tell me what this is and how to fix?

Thanks

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
macro for work book Daniel Charts and Charting in Excel 1 August 12th 07 12:04 AM
How to work with macro in background Jared Excel Discussion (Misc queries) 2 August 14th 06 04:49 AM
Macro doesn't work but recording it does. Rob Excel Discussion (Misc queries) 2 May 24th 05 06:43 AM
Why does the macro not work? Xanadude Excel Worksheet Functions 4 April 24th 05 09:06 PM
how do you get (end down) to work in a macro? lcx2 Excel Worksheet Functions 1 March 24th 05 08:15 AM


All times are GMT +1. The time now is 02:41 AM.

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

About Us

"It's about Microsoft Excel"