PDA

View Full Version : need help in writting VBA Code to save file sequentially after each loop



daspoone
07-04-2018, 05:45 AM
good day,
so I'm not a coder by any stretch of my imagination but I've inherited a workbook that allows me run thousands rows of data, the VBA code transposes data from a sheet into another sheet that performs various calculations based on each row of data (it loops and performs the same function until the last row):at the end of the VBA code it performs a print function, now instead of printing I'd like it to save as a copy (preferably pdf) in sequential files in a specified folder.

The VBA coding currently ends with

shDest.PrintOut 'COPIES:=2

My poor attempt to VBA code a file save as option is below.

Dim filepath As String
Dim filename As String
Dim filepatharch As String
Dim filedate As String
Dim filelist As String

filedate = Format(Now, "MMDD01.")
filepath = "\\C:\Users\PTX\Desktop\FileDrop (file://\\C:\Users\PTX\Desktop\FileDrop)"
tdfilename = "TD" & filedate & filelist & ".xls"
Sheets("TDflatfile").Copy
ActiveWorkbook.SaveAs filename:="\\C:\Users\PTX\Desktop\FileDrop (file://\\C:\Users\PTX\Desktop\FileDrop)"" & tdfilename, FileFormat:=xls,"

Any ideas or suggestions would be greatly appreciated.

MINCUS1308
07-04-2018, 11:34 AM
Here is a great reference for the .SaveAs method: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-saveas-method-excel

MINCUS1308
07-04-2018, 11:38 AM
What is FileList? the variable is not defined?

MINCUS1308
07-04-2018, 11:49 AM
Sub InterestingTest()
'...
'YOU PROBABLY HAVE STUFF BEFORE THE LOOP
'...

'THEN YOUR LOOP STARTS
'NOTE THE VARIABLE 'i'**
For i = 1 To 10
'YOULL HAVE WHAT EVER HAPPENS IN YOUR LOOP
'...

'THEN INSTEAD OF PRINTING YOU WILL HAVE SOMETHING LIKE THE FOLLOWING:
Dim FilePath As String
Dim FileDate As String
Dim FileList As String
Dim FileName As String
Dim FilePathArch As String

FilePath = "\\C:\Users\PTX\Desktop\FileDrop\ (file:///C:/Users/PTX/Desktop/FileDrop/)"
FileDate = Format(Now(), "MMDD01.")
FileList = i '**HERE IS WHERE THE INCREMENTAL FILE NAMING COMES INTO PLAY**
FileName = "TD" & FileDate & FileList & ".pdf"

Sheets("TDflatfile").Copy
ActiveWorkbook.SaveAs FileName:=FilePath & FileName

'YOU WILL PROBABLY HAVE THINGS AFTER YOU SAVE [PRINT]
'...

'AND THEN THE LOOP WILL 'LOOP'
Next i

'YOU WILL PROBABLY HAVE THINGS AFTER THE LOOP
'...

End Sub