Hey there, after feeling like a rock star by implementing a VBA script for my company, I've been thrown a new variable I don't know how to account for.
What I'm doing: I'm copying information from a .CSV that is imported into a folder that I have another .XLM file located at that has my script on it. User opens the XLM file, clicks the button to start the script, then that script is copying the data from the .CSV file, pasting it into itself, then exporting that newly formatted spreadsheet to a .txt file in a different folder, then deleting the original .CSV file that was imported so the next .CSV file can be imported without overwriting needing to be done
What has changed: I thought there would always and forever only be one CSV file being exported out of our other program into this folder, which would always be the current year - current month, IE 2021-11 if done today. I setup the script to read this as "yyyy-m". Now I've found out that if older information is being exported at the same time, that this program will export multiple files, each with the Year and the Month that the information inside contained. So now instead of only having 2021-11.csv in november, if older data is exported at the same time, I can have 2021-11.csv, 2021-10.csv, 2021-8.csv, and 2021-5.csv. (If January - September this number is formatted without a 0, IE 1 for January instead of 01)
Here's the script as I have it, how can I make it where the information is copied from ALL of the CSV files into the proper places into the XLM file and ready for export to the TXT file?
I am still very new to all things VBA and scripting in general (why so many comments in the code, my learning) really appreciate any assistance with this.
What I'm doing: I'm copying information from a .CSV that is imported into a folder that I have another .XLM file located at that has my script on it. User opens the XLM file, clicks the button to start the script, then that script is copying the data from the .CSV file, pasting it into itself, then exporting that newly formatted spreadsheet to a .txt file in a different folder, then deleting the original .CSV file that was imported so the next .CSV file can be imported without overwriting needing to be done
What has changed: I thought there would always and forever only be one CSV file being exported out of our other program into this folder, which would always be the current year - current month, IE 2021-11 if done today. I setup the script to read this as "yyyy-m". Now I've found out that if older information is being exported at the same time, that this program will export multiple files, each with the Year and the Month that the information inside contained. So now instead of only having 2021-11.csv in november, if older data is exported at the same time, I can have 2021-11.csv, 2021-10.csv, 2021-8.csv, and 2021-5.csv. (If January - September this number is formatted without a 0, IE 1 for January instead of 01)
Here's the script as I have it, how can I make it where the information is copied from ALL of the CSV files into the proper places into the XLM file and ready for export to the TXT file?
VBA Code:
Sub paste_values()
'Fname = Filename, Fpath - Filepath, Sname = Sheetname
Dim FName As String
Dim FPath As String
Dim Sname As String
Dim wb1 As ThisWorkbook
FPath = "C:\FOLDER\"
FName = Format(Date, "yyyy-m") & ".csv"
Sname = Format(Date, "yyyy-m")
'open source workbook
Workbooks.Open Filename:=FPath & FName
'copying data
Workbooks(FName).Worksheets(Sname).Range("A2:I200").Copy
'define destination and paste values
Workbooks.Open "C:\FOLDER\Script.xlsm"
ThisWorkbook.Sheets("Sheet1").Range("B2").PasteSpecial Paste:=xlPasteValues
'suppress overwrite prompt
Application.DisplayAlerts = False
'Export data to TXT file
ActiveSheet.SaveAs "C:\NEWFILEEXPORT\Export.txt", FileFormat:=xlTextPrinter
'Enable prompts again
Application.DisplayAlerts = True
'Display Export complete message
x = MsgBox("Export Completed Successfully", 0, "Export")
'Close open workbooks
Workbooks(FName).Close
'Delete all CSV files to clear folder out for next Docuware Export file
Kill "C:\Folder\*.csv*"
'Close current workbook
ActiveWorkbook.Close
End Sub
I am still very new to all things VBA and scripting in general (why so many comments in the code, my learning) really appreciate any assistance with this.