autosave keeps saving as "true" or "false" for filename - excel

something is wrong with the macro, it keeps saving the way i want it to, but names the file "true" or "false". Note: the cell value itself is "=today()"
Sub Macro1()
'
' Macro1 Macro
ActiveWorkbook.SaveAs Filename = Range("C6").Value
FileFormat = xlOpenXMLWorkbookMacroEnabled
CreateBackup = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF
Filename = Range("c6").Value
FileFormat = pdf
Quality = xlQualityStandard
IncludeDocProperties = True
IgnorePrintAreas = False
OpenAfterPublish = False
End Sub

Try Filename:=Range("c6").Text to get the date as it appears in the cell.
The operator syntax is := between argument name and argument value.
Sub Macro1()
ActiveWorkbook.SaveAs _
Filename:=Range("C6").Text, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
CreateBackup:=False
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Range("c6").Text, _
FileFormat:=xlpdf, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub

Related

if-statement with two conditions is ignored

It´s probably a too easy question for a forum, but I´m kinda hard stuck and my prog skills are very limited. After submitting a userform in vba, I want the program to check if the user made a logic mistake. There are several CheckBoxes and a combination of selections is possible, but not all combinations are allowed. So I have written the following code to prevent the user from submitting the form with selecting two specific CheckBoxes.
But the program completely ignores this if-statement and no matter what CheckBoxes are selected, nothing changes.
If userform1.CheckBoxOption3.Value = True And userform1.CheckBoxOption7.Value = True Then
MsgBox "This Combination is not possible!", vbCritical
Exit Sub
End If
Am I missing something obvious?
Edit: Additional Code:
Private Sub CmdFertig_Click()
Worksheets("sheets1").Range("I22").Value = userform1.TextBox1.Value & "°"
Worksheets("sheets1").Range("I13").Value = userform1.TextBox2.Value & "°"
Worksheets("sheets1").Range("E17").Value = userform1.TextBox3.Value & "°"
If userform1.CheckBox1.Value = True Then
Worksheets("sheets1").Range("g24").Value = userform1.TextBox1.Value & "°"
End If
If userform1.CheckBox2.Value = False Then
Worksheets("sheets1").Range("f24").Value = ""
Worksheets("sheets1").Range("f25").Value = ""
End If
If userform1.CheckBox3.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Wechselseitig"
End If
If userform1.CheckBox5.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Einseitig"
End If
If userform1.CheckBox7.Value = True Then
Worksheets("sheets1").Range("h25").Value = "Im UZ voreilend"
End If
If userform1.CheckBox3.Value = True And userform1.CheckBox7.Value = True Then
MsgBox "This Combination is not possible!", vbCritical
Exit Sub
End If
userform1.Hide
Worksheets("sheets1").ExportAsFixedFormat _
Type:=xlTypePDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
Filename:=userform1.Text1.Value & ".pdf", _
OpenAfterPublish:=True
Dim sPath As String
sPath = "O:\F1\completed\"
With Worksheets("sheets1")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPath & userform1.TextBox1.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
SetAttr sPath & userform1.TextBox1.Value & ".pdf", vbReadOnly
End Sub
As a first step like suggestd in my comments I would remove the references to the default instance in the code itself. Like written here
A UserForm is essentially a class module with a designer and a
VB_PredeclaredId attribute. That PredeclaredId means VBA is
automatically creating a global-scope instance of the class, named
after that class.
UserForm1.Show at the call site, where UserForm1 isn’t a local
variable but the “hey look it’s free” default instance, which means
you’re using an object without even realizing it (at least without
New​-ing it up yourself) – and you’re storing state that belongs to a
global instance, which means you’re using an object but without the
benefits of object-oriented programming.
So removing all references to the default instance in the code might already help. For the posted code
Private Sub CmdFertig_Click()
Worksheets("sheets1").Range("I22").Value = TextBox1.Value & "°"
Worksheets("sheets1").Range("I13").Value = TextBox2.Value & "°"
Worksheets("sheets1").Range("E17").Value = TextBox3.Value & "°"
If CheckBox1.Value = True Then
Worksheets("sheets1").Range("g24").Value = TextBox1.Value & "°"
End If
If CheckBox2.Value = False Then
Worksheets("sheets1").Range("f24").Value = ""
Worksheets("sheets1").Range("f25").Value = ""
End If
If CheckBox3.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Wechselseitig"
End If
If CheckBox5.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Einseitig"
End If
If CheckBox7.Value = True Then
Worksheets("sheets1").Range("h25").Value = "Im UZ voreilend"
End If
If CheckBox3.Value = True And CheckBox7.Value = True Then
MsgBox "This Combination is not possible!", vbCritical
Exit Sub
End If
Hide
Worksheets("sheets1").ExportAsFixedFormat _
Type:=xlTypePDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
Filename:=Text1.Value & ".pdf", _
OpenAfterPublish:=True
Dim sPath As String
sPath = "O:\F1\completed\"
With Worksheets("sheets1")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPath & TextBox1.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
SetAttr sPath & TextBox1.Value & ".pdf", vbReadOnly
End Sub
I also suggest to read about
VBA Userform
Addvanded Tips on Userform

VBA code for Print to PDF (print pages 1 to 3 only)

I have the below code, and I am here to ask what I would need to add to the code to print pages 1 to 3 only?
Sub PrintVisa()
Dim invoiceRng As Range
Dim pdfile As String
'Setting range to be printed
Set invoiceRng = Range("C7:L175")
pdfile = " Seabourn_Visa_Letter"
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
Please, try the next code:
Sub ExportFirstThreeSheets()
Dim FileName As String
FileName = ThisWorkbook.Path & "\MyThreeSheets.pdf"
Sheets(Array(1, 2, 3)).copy 'it creates a new workbook containing the first three sheets
With ActiveWorkbook
.ExportAsFixedFormat xlTypePDF, FileName, , , , , , True
.Close False
End With
End Sub
The ExportAsFixedFormat has a page range built in. See below:
Sheet5.ExportAsFixedFormat Type:=xlTypePDF, Filename:="YouFileName.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, From:=1, To:=3, OpenAfterPublish:=True

Save range of cells to PDF at ThisWorkbook.Path

I have this macro that saves to a defined path which is not useful since multiple people will be using the file.
Sub SavetoPdf()
'
' SavetoPdf Macro
' To Save to PDF Details
'
' Keyboard Shortcut: Ctrl+s
'
Range("E31:I54").Select
Application.CutCopyMode = False
ChDir "/Users/Me/Desktop/"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/Me/Desktop/" & Range("G41").Value & ".pdf", Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub
I thought to save to ThisWorkbook.Path and modified the above.
Sub SavetoPdf()
' Saves active sheet as PDF file.
Dim Name As String
Name = ThisWorkbook.Path & "/" & Range("G41").Value & ".pdf"
Range("E31:I54").Select
Application.CutCopyMode = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Name, _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
I am on a Mac.
I get
Error on Printing
and the debugger highlights this code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Name, _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
You title reads that you want to PDF a "Range" of cells. Is that what is not working for you?
Sub SavetoPdf()
' Saves active sheet as PDF file.
Dim Name As String
Name = ThisWorkbook.Path & "/" & Range("G41").Value & ".pdf"
Range("E31:I54").ExportAsFixedFormat Type:=xlTypePDF, Filename:=Name, _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
Modified #Davesexel code: Tested on mock data and saved no problem.
Dim rng As Range
Dim Name As String
Set rng = Worksheets("Sheet1").Range("G41") 'Change worksheet to your needs
Name = rng.Value
Range("E31:I54").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "/" & Name & ".pdf", _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False

Save Excel sheet to pdf

I have a macro that would save a sheet to pdf format but it doesn't work anymore.
With ActiveSheet.PageSetup
.CenterHeader = strFile2
.Orientation = xlPortrait
.PrintArea = "a1:q21"
'.PrintTitleRows = ActiveSheet.Rows(5).Address
'.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
End With
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
I tried
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating
It gives the same
invalid procedure call or argument.
The macro works at my colleague's PC.
Have you declared variables outside of the snippet of code you pasted? You need to declare ws as a worksheet or you are going to have issues:
Dim ws as Worksheet
You need to set ws = ActiveSheet in order to use it, though I would recommend avoiding ActiveSheet since it can lead to issues. Something better would be:
Set ws = Workbooks("**Name of your workbook**").Worksheets("**Name of worksheet**")
With ws.PageSetup
**code**
End With
Hope that helps.

VBA Error: "Compile error: Expected End Sub"

Trying to pass "GetFullNamePDF()" to the Filename attribute, but getting the following error: "Compile error: Expected End Sub"
Sub PrintPDF()
Function GetFullNamePDF() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
I know nothing about VBA, and got the above code from a question I asked yesterday, but was unable to test at the time. Guessing the error has to do with the function, since the code works without the function added and the filepath/name hard coded.
Idea of the code is to dynamically use the filename of itself to name the path and file for the PDF. If you have any questions, just comment -- thanks!
You can't nest a function inside a procedure. You need to move it above:
Function GetFullNamePDF() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
'This should be
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
'Remove the quotes from GetFullNamePDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
GetFullNamePDF(), Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
It is illegal to declare a function within a sub.
It should look like this:
Function GetFullNamePDF() As String
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
Like this:
Function GetFullNamePDF() As String
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
Dim sFileName As Variable
sFileName=GetFullNamePDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sFilename, Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Resources