' This VBScript code assumes the table starts in row 1 column 1 (cell A1) on the first sheet. Dim lngLastRow, lngLastCol, i, n Dim strTblName, strWorkBookName, strTmp Dim objExcel, objWorkBook, objApp dim lngXlUp, lngXlToLeft, lngXlSrcRange, lngXlYes ' make sure path to excel file is specified on command line If Wscript.Arguments.Count <> 1 Then Wscript.Echo _ "Please supply the path to an excel file on the command line" Wscript.Quit 1 End If ' Initialization strTblName = "Table1" strWorkBookName = WScript.Arguments.Item(0) Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open(strWorkBookName) Set objApp = objExcel.Application objApp.Visible = True objApp.CutCopyMode = False lngXlUp = -4162 ' objExcel.XlDirection.xlUp lngXlToLeft = -4159 ' objExcel.XlDirection.xlToLeft lngXlYes = 1 ' objExcel.XlYesNoGuess.xlYes lngXlSrcRange = 1 ' objExcel.XlListObjectSourceType.xlSrcRange ' Determine table geometry, assuming table starts at cell A1. lngLastRow = objApp.ActiveSheet.Cells(1048576, 1).End(lngXlUp).Row lngLastCol = objApp.ActiveSheet.Cells(1, 16384).End(lngXlToLeft).Column With objApp.ActiveSheet ' If strTblName exists in ListObjects, remove it so that this ' script is re-entrant (can't add the same table repeatedly in ' Excel). n = .ListObjects.Count For i = 1 To n If .ListObjects(i).Name = strTblName Then .ListObjects(i).Unlist Exit For End If Next ' Create the table reference and select it .ListObjects.Add( _ lngXlSrcRange, _ objExcel.Range("a1", objExcel.Cells(lngLastRow, lngLastCol)), _ , _ lngXlYes).Name = strTblName objExcel.Range(strTblName & "[#All]").Select ' Set table style this has dark blue headers, alternating blue, white rows and slicers/filters .ListObjects("Table1").TableStyle = "TableStyleMedium6" End With objExcel.Range(strTblName & "[#Headers]").Select With objExcel.Selection.Font .Name = "Arial Black" .Size = 12 .Bold = True End With ' Code to AutoFit the entire table Dim objCbeg, objCend Set objCbeg = objApp.ActiveSheet.Columns(1) Set objCend = objApp.ActiveSheet.Columns(lngLastCol) objApp.ActiveSheet.Range(objCbeg, objCend).Select objApp.ActiveSheet.Range(objCbeg, objCend).EntireColumn.AutoFit ' Code to Deselect table objExcel.Range("A1").Select ' Save and Quit objApp.ActiveWorkbook.Save() objApp.Quit Wscript.Quit 0