VBA to open a new workbook and save as the value from a cell in another workbook

Benzoli7

Board Regular
Joined
Jan 11, 2010
Messages
136
Can anyone help me with some code that will open a new workbook and save it in another folder with a file name that is derived from the value of a cell in another workbook?

The value that will create the name for the new workbook is located in cell A4 of a sheet named "GRAPHS" in a workbook named "AAA DATA".

The file path for the new workbook will be: S:\Trans\AAA Data\"CELL VALUE"

Thanks so much for your help.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Assuming the name of the workbook in cell A4 includes the file extension (.xls, .xlsx, ...)
Code:
Sub MakeNewBook()
Dim wB As Workbook
Dim nPath As String
nPath = "S:\Trans\AAA Data\" & ThisWorkbook.Sheets("GRAPHS").Range("A4").Value
Set wB = Workbooks.Add
With wB
    .SaveAs Filename:=nPath
End With
End Sub
 
Upvote 0
What would be the easiest way to refer to this new workbook. I'm guessing since its name is a cell in another workbook I should give that range some kind of name? If so, how do I do that?

The rest of this macro is going to switch back and forth many times between this new workbook and the one mentioned in the original post.

Thanks again.
 
Upvote 0
The Dim statement in the code I posted (Dim wB as Workbook) together with the line Set wB = Workbooks.Add has established wB as an object. Thereafter you can simply refer to wB and Excel will know you are referring to the new workbook. For example, wB.Activate will activate the new workbook if it is not currently active.
 
Upvote 0
Thanks again Joe.

One more question for you.

I want to publish this new workbook as a PDF file and give it the same name as the workbook that your code creates. I tried to (actual name) code that I got from the macro recorder with the cell referencing code that you provided earlier. Can you tell me how to manipulate this to make both the excel and pdf files have the same name created from the same cell in the original book.


The code below is what the recorder gave me. The part that says "2011-9-29" needs to be placed with a reference to that original cell.

HTML:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "O:\Transportation\Ben\TEST\2011-9-29.pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        True
Thanks again for you help.
 
Upvote 0
Assuming the sheet "GRAPHS" is the one that contains the file name in cell A4 try this: (note that if A4 includes the file extension (.xls, .xlsx, ...) it will be included in the pdf file name. if that's not what you want let me know.)
Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="O:\Transportation\Ben\TEST\" & _
    ThisWorkbook.Sheets("GRAPHS").Range("A4").Value & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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