I'm Trying to create a directory in excel-VBA and then copy the xlsm and ,pdf file into this directory.
I can create the directory but I can't seem to save the files into this directory?
Code below. Any Help is much appreciated. This is driving me crazy. All I'm doing is concatenating the filename and it's being saved to the c:\temp folder, but I want it to save it into a sub folder in c:\temp
Sub Macro2()
'
' Macro2 Macro
'
Dim FileName As String
Dim FileName2 As String
'FileName3 As String
FileName = Sheet1.TextBox1.Text
FileName2 = ("C:\TEMP\" & FileName)
'CheckDir As String
MsgBox (FileName2)
MkDir (FileName2)
ChDir (FileName2)
ActiveWorkbook.SaveAs FileName:=FileName2 & FileName & "2xlsm.xlsm" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName2 & "FileName" & "_2xlsm.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
enter image description here
The format of FileName2 should be fixed to properly concatenate FileName in the file creation. Also, the FileName argument of both SaveAs and ExportAsFixedFormat should be the same, assuming you want both the XLSM and PDF files to be saved in one directory.
Please refer to the modified code below:
Sub Macro2()
'
' Macro2 Macro
'
Dim FileName As String
Dim FileName2 As String
FileName = Sheet1.TextBox1.Text
FileName2 = "C:\TEMP\" & FileName & "\"
MsgBox (FileName2)
MkDir (FileName2)
ChDir (FileName2)
ActiveWorkbook.SaveAs FileName:=FileName2 & FileName & "2xlsm.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName2 & FileName & "_2xlsm.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
Your missing & "\" & (backslash) on FileName2 & "\" & FileName, _
See Complete code
Option Explicit
Sub Macro2()
'
' Macro2 Macro
'
Dim FileName As String
Dim FileName2 As String
FileName = Sheet1.TextBox1.Text
FileName2 = ("C:\TEMP\" & FileName)
MsgBox (FileName2)
MkDir (FileName2)
ActiveWorkbook.SaveAs FileName:= _
FileName2 & "\" & FileName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName2 & "\" & "FileName", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Or simply do this
FileName2 = ("C:\TEMP\" & FileName & "\")
Related
seems to almost work but the strlocation is showing me that there is no "\" between name of file and it's location. How do I add that.
Sub Macro1()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Range("f6").Text, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Set Mail_Object = CreateObject("Outlook.Application")
With Mail_Object.CreateItem(o)
.Subject = Range("f6").Text
.To = "EMAIL"
.Body = "Daily movement file attached" & Chr(13) & Chr(13) & _
"Regards," & Chr(13) & "SCD" & Chr(13) & "AAAAAA"
strlocation = "C:\Users\User\Desktop\macro save" & Range("f6").Text & ".pdf"
.Attachments.Add (strlocation)
.Send
End With
Set Mail_Object = Nothing
End Sub
So you're only missing a /?
Change this:
strlocation = "C:\Users\User\Desktop\macro save" & Range("f6").Text & ".pdf"
...to this:
strlocation = "C:\Users\User\Desktop\macro save\" & Range("f6").Text & ".pdf"
I am quite new to vba. I wrote a piece of code to save an excel workbook. the original save path works and the file gets saved there, but when i try to change it (to a shared directory) it doesn't listen.Instead, it just saves in My Documents. Below is the code that works.
Any help would be appreciated
Sheets("Sheet1").Select
Sheets("Sheet1").Copy
ChDir "C:\Users\SI\Desktop\Generator"
ActiveWorkbook.SaveAs Filename:= _
Range("B4").Value & " TRS - " & Range("E6").Value & " vs " _
& Range("E7").Value & " - " & Format(Date, "dd-mm-yyyy"), FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets("ST").Select
Just specify the full path in the Filename argument for the SaveAs
Sheets("Sheet1").Select
Sheets("Sheet1").Copy
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\SI\Desktop\Generator\" & Range("B4").Value & " TRS - " & Range("E6").Value & " vs " _
& Range("E7").Value & " - " & Format(Date, "dd-mm-yyyy"), FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets("ST").Select
I have managed to get this to save as a PDF using 'ExportAsFixedFormat', but when I try to use 'SaveAs' to get a CSV (although I would accept xlsx too!) I get:
Run time error: 1004 Application defined or Object defined error
I have spent way to long trying to do this and can't find any answers that work for me online...
Sub SaveTrackingSheet()
Dim NewPathTrack As String
NewPathTrack = Application.ThisWorkbook.Path & "\PDF Outputs\" & Range("NameTrack").Text & "\"
If Dir(NewPathTrack, 63) = "" Then MkDir NewPathTrack
Dim NewPathDealer As String
NewPathDealer = NewPathTrack & Range("CodeTrack").Text & " - " & Range("NameTrack").Text & "\"
If Dir(NewPathDealer, 63) = "" Then MkDir NewPathDealer
Sheets("Tracking Sheet").SaveAs _
Filename:=NewPathDealer & "\" & Range("CodeTrack").Text & " - Tracking Sheet" & ".csv", _
FileFormat:=xlCSV, _
ConflictResolution:=2, _
Local:=True, _
CreateBackup:=False
End Sub
This is the line that errors:
Sheets("Tracking Sheet").SaveAs _
Filename:=NewPathDealer & "\" & Range("DealerCodeTrack").Text & " - Tracking Sheet" & ".csv", _
FileFormat:=xlCSV, _
ConflictResolution:=2, _
Local:=True, _
CreateBackup:=False
Thanks in advance!
fileformat:=xlCSVMSDOS
Check fileformat specifications at https://msdn.microsoft.com/es-es/vba/excel-vba/articles/xlfileformat-enumeration-excel
UPDATED RIGHT NOW:
The code that works for me is:
Sub SaveTrackingSheet()
Dim NewPathTrack As String
NewPathTrack = Application.ThisWorkbook.Path & "\PDF Outputs\" & Range("NameTrack").Text & "\"
If Dir(NewPathTrack, 63) = "" Then MkDir NewPathTrack
Dim NewPathDealer As String
NewPathDealer = NewPathTrack & Range("CodeTrack").Text & " - " & Range("NameTrack").Text & "\"
If Dir(NewPathDealer, 63) = "" Then MkDir NewPathDealer
Sheets("Tracking Sheet").SaveAs _
Filename:=NewPathDealer & Range("CodeTrack").Text & " - Tracking Sheet" & ".csv", _
FileFormat:=xlCSV, _
Local:=True, _
CreateBackup:=False
'ConflictResolution:=2 This line causes the error
End Sub
Hope it helps!
I made a macro to export my xlsm file into a csv file. It works great on one computer where the directory of the server is "I" but on another computer with the same server saved to directory "T" it fails. Is there a solution to this multi-directory/multi-computer problem? The trimmed code is attached with the directory line pointed out.
Sub ExportAsCSV()
Dim Answer As VbMsgBoxResult, Dir As String, LastRow As Long, _
Date1 As Date, Date2 As Date, CSVFileName As String
' *********************************************************
' Directory String <---------------- The Issue
Dir = "I:\2017\CVS" ' Could be "I:\", could be "T:\" ...
' *********************************************************
' Creating the Name of the CSV File using the _
' first and last date in column C (C1 is a header)
LastRow = Cells(Rows.Count, "C").End(xlUp).Row
Date1 = Range("C2").Value
Date2 = Cells(LastRow, "C")
If Date1 < Date2 Then
CSVFileName = "FILE." & Format(Date1, "mm.dd.yy") & _
"-" & Format(Date2, "mm.dd.yy") & ".csv"
ElseIf Date1 > Date2 Then
CSVFileName = "FILE." & Format(Date2, "mm.dd.yy") & _
"-" & Format(Date1, "mm.dd.yy") & ".csv"
Else
CSVFileName = "FILE." & Format(Date1, "mm.dd.yy") & ".csv"
End If
' Double Check User wants to make a sheet Response
Answer = MsgBox("Clicking 'Yes' will create a CSV file named " & vbCrLf & vbCrLf & _
" " & CSVFileName & vbCrLf & vbCrLf & _
"into " & vbCrLf & vbCrLf & " " & Dir & vbCrLf & vbCrLf & _
"It will overwrite any CSV with an identical name." & vbCrLf & vbCrLf & _
"Is this what you want to do?", vbYesNo + vbQuestion)
'Act based on the Response
If Answer = vbYes Then
' Ready all cells for csv creation
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
ws.Copy
ActiveWorkbook.SaveAs FileName:=Dir & "\" & CSVFileName, _
FileFormat:=xlCSV, CreateBackup:=False
MsgBox ("Created the csv file:" & vbCrLf & vbCrLf & _
Dir & "\" & CSVFileName)
Else
MsgBox ("Did not create the csv file.")
End If
End Sub
Any help is appreciated.
You need to use a UNC path instead of a mapped network drive.
Dir = "\\ServerName\SomeFolder\2017\CVS"
If you don't know what server/folder the network drive is pointing to, ask your network administrator.
Side note
You shouldn't name things in ways that hide/shadow identifiers that already exist in global namespace: Dir is actually a function in the VBA.FileSystem module; by declaring a Dir local variable, you make the name potentially ambiguous to the reader (although the compiler doesn't care).
I am trying to print an active sheet as a PDF, is it also possible to print page 1 and 2 separately? i have this so far. i want the name of the export to use the values from cells B1 I1 and J1
Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Set ws = ActiveSheet
'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
& "_" _
& Range("B1"&"I1"&"J1") _
& ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "PDF file has been created."
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Just use the From and To parameters of the ExportAsFixedFormat function:
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False, _
From:=1, _
To:=1
and the same again with page 2