Auto-Run Macro When Saving?

JFry08

New Member
Joined
Oct 12, 2009
Messages
7
I am looking for a way to have a macro run every time a file is saved? More specifically, I am developing a file that can be used on our receiving dock, and everytime an item is received and they put an 'x' into the 'Received' column, I want my macro that copies that row onto another sheet.

Thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Put the following under ThisWorkbook in the VBAProject window

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Your Code Here
End Sub
 
Upvote 0
I have tried the below code but it did not work with me when saving, bearing in mind that I am junior user of VBA. thanks

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sub RenameTabs()
'Updateby20140624
For x = 1 To Sheets.Count
If Worksheets(x).Range("A1").Value <> "" Then
Sheets(x).Name = Worksheets(x).Range("A1").Value
End If
Next
End Sub
 
Upvote 0
than you should tell us what you want:
your code is Naming your sheets by taking values from cell A1 in each sheet of your workbook.
may be you did not locate this Private Sub in "Microsoft Excel Objects\This Workbook"?
 
Upvote 0
Your speech is right and I agree with you totally however, the issue here that I want TO RUN the above code when pressing(save button) but it didn't work however,when I press view macros and then press run it woks.

finally, I want to summarize my long speech that "I want my code to run automatically when pressing save"

thanks and best regards,
 
Upvote 0
well, in my version of excel (2013) it work either I press Ctrl+S or click on save icon.
are you sure you put
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
For x = 1 To Sheets.Count
If Worksheets(x).Range("A1").Value <> "" Then
Sheets(x).Name = Worksheets(x).Range("A1").Value
End If
Next
End Sub
NOT in separate module, and NOT in any worksheet, BUT in ThisWorkbook

PS you have 2 folders in VBA window:
Microsoft Excel Objects and Modules. you need first one and find there ThisWorkbook or something similar //I have not english excel, so translation could vary a bit
 
Last edited:
Upvote 0
First of all, I would like to thank you for your kindness and your help which is highly appreciated.

in the meantime, I would like to ask if there's a way to upload my file here for your review.

sorry for the disturbance.

Best Regards,
 
Upvote 0

Forum statistics

Threads
1,216,389
Messages
6,130,323
Members
449,573
Latest member
bengee54

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top