VBA to create a copy of a worksheet on my desktop but with values rather than formulas

barnabyr

New Member
Joined
Mar 6, 2019
Messages
9
Hi, I have written my first VBA macro using the macro recorder. I understand that the recorder adds a lot of unnecessary code but I needed to start learning somewhere!
The macro currently creates an export PDF on my desktop of a specific worksheet, after entering specific values in some cells & applying an advanced filter.
The macro runs through a few different iterations, saving around 30 PDFs on my desktop with different cell values & filters. The PDFs show specific information based on geographical locations.
I've been asked to change it so that rather than creating a PDF it creates a flat excel worksheet with values & formatting rather than data.
So far I have this:

Sub Station_Print()

Dim FilePath As String, dt As String

FilePath = CreateObject("WScript.Shell").specialfolders("Desktop")
dt = Format(CStr(Now), "yyyy-mm-dd")

FIRST ITERATION STARTS HERE

Sheets("As Built Tracker").Select

Range("B2").Select
ActiveCell.FormulaR1C1 = "Station Code"
Range("C2").Select
ActiveCell.FormulaR1C1 = "Station Description"
Range("B3").Select

Range("abt_tracker").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange _
:=Range("af_lis"), Unique:=False

Debug.Print FilePath

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=FilePath & "\File Name 1 - " & dt & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

FIRST ITERATION ENDS HERE
THEN LOADS OF ITERATIONS GO HERE


Sheets("As Built Tracker").Select

Range("B2").Select
ActiveCell.FormulaR1C1 = "Project Wide"
Range("C2").Select
ActiveCell.FormulaR1C1 = "Project Location"
Range("B3").Select

ActiveSheet.Range("abt_tracker").AutoFilter Field:=2
ActiveSheet.Range("abt_tracker").AutoFilter Field:=3

End Sub

I've added my comments in caps for this post (I imagine there's a way of commenting in the code directly but I haven't got that far yet!)
So if there's a bunch of bloated code I'm obviously happy to cut it out but my main query I'm stumped on, is how to save the worksheet on my desktop & paste the values & formatting in to it.

Sorry if I've rambled on too much!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You can try this. It will create a new workbook by copying the reformatted sheet and then the data will be changed to values only (no formulas) on the new worksheet. Both workbooks remain open.

Code:
Sub Station_Print2()
Dim FilePath As String, dt As String
FilePath = CreateObject("WScript.Shell").specialfolders("Desktop")
dt = Format(Date, "yyyy-mm-dd")
    With Sheets("As Built Tracker")
        .Range("B2") = "Station Code"
        .Range("C2") = "Station Description"
        .Range("abt_tracker").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange _
        :=Range("af_lis")
        .Copy
    End With
    With ActiveWorkbook.Sheets(1).UsedRange
        .Value = .Value
    End With
    With Sheets("As Built Tracker")
        .Range("B2") = "Project Wide"
        .Range("C2") = "Project Location"
        .Range("abt_tracker").AutoFilter Field:=2
        .Range("abt_tracker").AutoFilter Field:=3
    End With
End Sub
 
Upvote 0
Thanks so much for the speedy reply JLGWhiz!
That is getting much closer to what I'm after but is there a way to save the values worksheet with a different name & not have it open?
I need to create about 30 of these worksheets for the different locations so I would like to just create a copy on the desktop with a specific name & not have it open.
 
Upvote 0
This will allow you to enter a name for your new workbook, and the sheet will be the same name. It will be saved using your FilePath variable as the path.

Code:
Sub Station_Print2()
Dim FilePath As String, dt As String
FilePath = CreateObject("WScript.Shell").specialfolders("Desktop")
dt = Format(Date, "yyyy-mm-dd")
    With Sheets("As Built Tracker")
        .Range("B2") = "Station Code"
        .Range("C2") = "Station Description"
        .Range("abt_tracker").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange _
        :=Range("af_lis")
        .Copy
    End With
    With ActiveWorkbook.Sheets(1).UsedRange
        .Value = .Value
RETRY:
        nm = InputBox("Enter a name for the new workbook.  Do Not include file extension.", "WORKBOOK NAME")
            If nm = "" Then
                MsgBox "You must enter a file name.", vbCritical, "MANDATORY ENTRY"
                GoTo RETRY:
            End If
        ActiveSheet.Name = nm
        ActiveWorkbook.SaveAs FilePath & "\" & nm & ".xlsx"
        ActiveWorkbook.Close False
    End With
    With Sheets("As Built Tracker")
        .Range("B2") = "Project Wide"
        .Range("C2") = "Project Location"
        .Range("abt_tracker").AutoFilter Field:=2
        .Range("abt_tracker").AutoFilter Field:=3
    End With
End Sub

I am sure you realize that I have no idea how you name your files and worksheets or even how your files are created and managed, so if you if the code needs to be modifed to include those type things, they have to be spelled out in the posts to the this thread, unless you can do them yourself.
 
Last edited:
Upvote 0
JLGWhiz you are the absolute man!
My apologies for not being clear enough.
I can’t really share the file name due to confidentiality issues but in my original code where it says:
Filename:=FilePath & "\File Name 1 - " & dt & ".pdf"
Where it says “\File Name 1 - “ that would be then “\File Name 2 - “ in the next iteration, so the filenames are hard coded in the vba rather than a naming prompt.
I was thinking originally I could just change the export line from a PDF to xls but that didn’t work.
Thanks again for your reply, it’s definitely getting closer to what I had in mind!
 
Upvote 0
JLGWhiz you are the absolute man!
My apologies for not being clear enough.
I can’t really share the file name due to confidentiality issues but in my original code where it says:
Filename:=FilePath & "\File Name 1 - " & dt & ".pdf"
Where it says “\File Name 1 - “ that would be then “\File Name 2 - “ in the next iteration, so the filenames are hard coded in the vba rather than a naming prompt.
I was thinking originally I could just change the export line from a PDF to xls but that didn’t work.
Thanks again for your reply, it’s definitely getting closer to what I had in mind!

You're welcome,
regards, JLG
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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