I have a Powershell script that will generate a CSV file with all available storage space for 18+ servers. I am now trying to automatically format the CSV file so it's a lot nicer to look at. My problem is that my code runs successfully once but then throws the 1004 error on a second run.
My code looked a lot more choppy and less polished prior to coming to StackOverflow. I've now removed some other mistakes like using Activesheet unnecessarily, using .Select, etc, but I now still cannot find the issue.
Please see my code below (please note the AutoFit part that I've commented out is a part I'm yet to get working).
Sub A_AllToTable()
Dim tbl As ListObject
With Sheets(1)
Set tbl = .ListObjects.Add(xlSrcRange, .UsedRange, , xlYes)
tbl.TableStyle = "TableStyleDark11"
' tbl.Columns.AutoFit
End With
End Sub
When I open the Workbook from fresh and run the code, it runs as expected. It will format the table. I'm hoping to get it to AutoFit as well as automatically sort by size, but for now I just want the code to work more than once
Could someone please provide some insight into why this isn't working so I can avoid making the same mistake again in the future?
Set Sheets = ThisWorkbook.Worksheets? Also have you checked your variables in the local window? That might give you some hint about the error and where it occurs.Set wks = ThisWorkbook.Worksheets(1)orSet wks = ThisWorkbook.Worksheets("Sheet1")for example....