Skip to content

Instantly share code, notes, and snippets.

@zelon88
Created September 9, 2019 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zelon88/61977e647b0df6219a720e38fe2cdd3f to your computer and use it in GitHub Desktop.
Save zelon88/61977e647b0df6219a720e38fe2cdd3f to your computer and use it in GitHub Desktop.
Helped out a Reddit user with some VBS...
'COMPLETELY UNTESTED!!!
'Modified by Reddit/GH user @zelon88 on 9/4/2019
'Set global variables.
'--------------------
'Change the masterSpreadsheet variable to the absolute path of your master spreadsheet.
masterSpreadsheet = "C:\DIR TO FILE\FILE.xlsx"
'--------------------
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = Wscript.Arguments.Item(0)
'Check to be sure the arguments passed are not blank.
If sFolder = "" Then
Wscript.Echo "Please set the absolute path to a folder containing only CSV files as the only argument to this script."
Wscript.Quit
End If
'Gather the items in the target folder in an array.
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
'Iterate through all the files in the specified folder.
For each csvFile In files
'Only process files with .csv in the filename.
If InStr(UCase(csvFile), UCase(".csv")) < 0 Then
'Open Excel
Set objExcel = CreateObject("Excel.Application")
'set to visible for testing set to false for production
objExcel.Visible = False
'opening the CSV and proper worksheet
Set objWorkbook = objExcel.Workbooks.Open(csvFile)
Set objWorksheet = objWorkbook.Worksheets("TAB")
'deleting header row from CSV
objWorksheet.Rows("1:1").Delete
'copying entire worksheet
Set objRange = objWorksheet.Range("A1:AQ1").EntireColumn
objRange.Copy
'Open destination Excel file and set worksheet
Set objExcel2 = CreateObject("Excel.Application")
'set to visible for testing set to false for production
objExcel2.Visible = False
'set variable for worksheet and workbook
Set objWorkbook2 = objExcel2.Workbooks.Open(masterSpreadsheet)
Set objWorksheet2 = objWorkbook2.Worksheets("TAB")
'paste data
lastrow = objWorksheet2.UsedRange.Rows.Count + 1
objWorksheet2.Range("A" & lastrow).PasteSpecial -4163
'Message Box used to create a pause to watch progress, disabled for production
'result=Msgbox("Are you sure?",vbYesNo+vbInformation, "")
'Unhide TAB worksheet for processing
objWorksheet2.Visible = True
'Sort descending by ID column
objWorksheet2.Activate
Const xlDescending = 2
Const xlYes = 1
'Set objRange2 = objWorksheet2.UsedRange
Set objRange3 = objExcel2.Range("E1")
objWorksheet2.UsedRange.Sort objRange3, xlDescending, , , , , , xlYes
'remove duplicate rows if duplicates found in ID column
objWorksheet2.UsedRange.RemoveDuplicates 5, xlYes
'Message Box used to create a pause to watch progress, disabled for production
'result=Msgbox("Are you sure?",vbYesNo+vbInformation, "")
'Hide TAB sheet
objWorksheet2.Visible = False
'Refresh PivotTable Data
Set objWorksheet3 = objWorkbook2.Worksheets("PivotTables")
objWorksheet3.PivotTables("PivotTable1").PivotCache.Refresh
'Close Excel
objExcel.DisplayAlerts = False
objExcel.Quit
objWorkbook2.save
End If
Next
objExcel2.Quit
MasterSpreadsheeter.vbs "C:/Path/To/Your/CSVs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment