[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

save as code

Posted on 2011-10-24
3
Medium Priority
?
284 Views
Last Modified: 2012-05-12
I have the following code below that selects sheets if there is a value in C4 on each sheet in my workbook and saves them as a PDF. What I need to do is make it change the save as name to C2 & J2 on sheet "Sht 1 - Table 1" C2 being a name and J2 being a date.

Thanks
Rob.


Sub SaveSheetsAsPDF()
  Dim ws As Worksheet
  Dim sSheet As Worksheet
  Dim SheetsFound()
  ReDim SheetsFound(0)
    For Each sSheet In ActiveWorkbook.Worksheets
        If (sSheet.Range("C4").Value) >= 1 Then
            'Debug.Print sSheet.Name & " has data"
            SheetsFound(UBound(SheetsFound)) = sSheet.Name
            ReDim Preserve SheetsFound(UBound(SheetsFound) + 1)
        End If
    Next sSheet
    Debug.Print VarType(SheetsFound)
 ReDim Preserve SheetsFound(UBound(SheetsFound) - 1)
  Sheets(SheetsFound).Select
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        ActiveWorkbook.Path & "NEEDS TO BE C2 AND J2 and save as pdf.pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
End Sub
0
Comment
Question by:RobJanine
  • 2
3 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 37022059
Change the code to:

 
Dim saveFileName As String
Dim sWks As Worksheet

  Set saveAsSheet = Sheets("Sht 1 - Table 1")
  
  saveFileName = ActiveWorkbook.Path & "\" & sWks.Range("C2").Value & Format(sWks.Range("J2").Value, "MM-DD-YYYY") & ".PDF"

  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveFileName _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True

Open in new window


Dave
0
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 37022068
My bad - the saveAsSheet should be sWks...

Here's the modified code:

 
Dim saveFileName As String
Dim sWks As Worksheet

  Set sWks = Sheets("Sht 1 - Table 1")
  
  saveFileName = ActiveWorkbook.Path & "\" & sWks.Range("C2").Value & Format(sWks.Range("J2").Value, "MM-DD-YYYY") & ".PDF"

  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveFileName _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True

Open in new window


Dave
0
 

Author Closing Comment

by:RobJanine
ID: 37022437
thanks

Rob
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question