Avatar of Dan Eckert
Dan EckertFlag for United States of America

asked on 

Excel macro. Record macro to save spreadsheet with unique name, using on a list of 20 individual spreadsheets in single folder.

I am an experienced Excel user who has never recorded a macro.  I've always followed a very intense click sequence to format and save 20 individual spreadsheets each week..

I'm trying to use the Record Macro function but have a problem.  Once I record a macro to save and try to run it on the next spreadsheet it renames the 2nd sheet with the name on the first sheet when the record button was on.  I want to name it to the name of the current sheet I'm running the save macro on.
I've never used VBA coding, and trying to avoid it.  I simply want each sheet to be saved with the name it had when opened after formatting..

Example:1st sheet name       6 14 21 AC 214 972 469.(recorded macro using this spreadsheet)
   2nd sheet name   6 14 21 AC 281 713 832.  Run save macro and it's renamed to AC 214 972 499
      and all 20 following spreadsheets.



Spreadsheets

Avatar of undefined
Last Comment
Dan Eckert
Avatar of Bembi
Bembi
Flag of Germany image

Hello,
the macro recorder just is recording, what you do with the mouse.
It is neither optimal nor it can built up a logic.
But a good start point to recognize objects you need to address.

If you save a file, the macro recorder just records the choosen name of the file and does the same with any file, where the macro is running.
So you have to relativate the macro and make it dependend from the Sheet Name.
The name of the saved file hast to be constructed from your actual environment.
ie NameToSave = ActiveSheet.Name or similar. 

As I can not say, what your macro recorder has recorded, it may be an option to post the file (with some empty, but named sheets and to post the file here, so we can correct the macro in the right way.
 
Avatar of Dan Eckert
Dan Eckert
Flag of United States of America image

ASKER

Bembi,  Thanks for your help.

Here is the saved macro I recorded.

The Bold File Name is the file I used to record the macro.

When i run the recorded macro on the next file (Test 2.xls) it saves as Test 1.xls.  I want it to save as Test 2.

Open in new window


A sample file is uploaded, named Test 7.xls.  When I run this macro on test 7 I want it to save as "Test 7.xls"

I want the macro to save it to the name of the open file.

I usually have 20 more files just like this, each with a unique name.




Sub Save_1()
'
' Save_1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
    ChDir "C:\Users\Dan\Desktop\SSS 2\Export Cust"
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\Dan\Desktop\SSS 2\Export Cust\06 14 21 test 1.xls", FileFormat:= _
        xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False
End Sub










06 14 21 test 7.xls



Avatar of Bembi
Bembi
Flag of Germany image

I put the names (path and file) into variables, just easier to change something if needed, and thrown out unneeded options

Sub Save_1()
'
' Save_1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
Dim filename As String
Dim filepath As String

filepath = "C:\Users\Dan\Desktop\SSS 2\Export Cust\"
filename = ActiveSheet.Name & ".xls"

    ActiveWorkbook.SaveAs filename:=filepath & filename, FileFormat:=xlExcel8
    
End Sub

Open in new window

Avatar of Dan Eckert
Dan Eckert
Flag of United States of America image

ASKER

Bambi, Thanks again for your help.
I'm having trouble testing your macro.  I've copied it exactly, to my Personal.xlsb file and running it.  It will not close the file in the .xls format.  It gives me the message that it can't save in the .xlsx format and I have to manually go back and switch the format to .xls.  Can we fix that?
My manual click path is "save as", click to change format to .xls, save , close file.
Thanks, Dan
ASKER CERTIFIED SOLUTION
Avatar of Bembi
Bembi
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Dan Eckert
Dan Eckert
Flag of United States of America image

ASKER

Hi Bambi,
My apologies for being so slow in getting back.  Sometimes life gets in the way of doing what I want to do.
I have been able to use your macro successfully.  It's doing what I want successfully.What we have saves me a couple clickss times 20 spreadsheets every Monday.
I did forget one more step.  When I run the macro a message pops up asking if I want to save "yes" or "no".  The active is set to no and I have to switch to yes,. and then click X to close the worksheet.  Two more clicks x 20 spreadsheets.Can we add that to the macro.
Thanks, It's a nice shortcut that saves me time.  Dan
Avatar of Bembi
Bembi
Flag of Germany image

The message box possibly mean, that it ask if you wan tto run macros?
This you can change in the  security settings.
Tools, Options, Trust Center, Click on Trust Center Settings...
I assume Macro Settings....

The second (closing Excel) you can realize by
ActiveWorkbook.Close
or
ActiveWorkbook.Close False (if you get another message)
als the last line
 
Avatar of Dan Eckert
Dan Eckert
Flag of United States of America image

ASKER

Bembi,  Thanks that did it.  Dan
Spreadsheets
Spreadsheets

A spreadsheet is an interactive computer application program for organization, analysis and storage of data in tabular form. Spreadsheets developed as computerized simulations of paper accounting worksheets. The program operates on data represented as cells of an array, organized in rows and columns. Each cell of the array may contain either numeric or text data, or the results of formulas that automatically calculate and display a value based on the contents of other cells.

7K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo