I am trying to apply VBA code to really large sheets. The idea is that the "My Portfolio.xlsx" filename used below will become a variable so I can run the code against any sheet without having to copy-paste the macro into each sheet.
I can convert a text column to a number datatype.
Sub ConvertTextToNumber()
Workbooks("My Portfolio.xlsx").Sheets("Sheet1").Range("A2:A9999").NumberFormat = "General"
End Sub
This works even if I change the filename. So the general format should be correct.
I am trying to sort the first column (with header) from smallest to largest.
Sub SortSmallestToLargest()
Workbooks("My Portfolio.xlsx").Sheets("Sheet1").Range("A2:BT9999").Sort Key1:=Range("A:A"), Order1:=xlAscending
End Sub
I've adjusted the Sort part multiple times, changing the Range from "A:A" to "A" to "A2", but none of them work.
I've also tried saying Header=Yes or Header=No, but it always errors.
I get
Run-time error '1004': Application-defined or object-defined error
My searches for that error usually refer to giving it out-of-bounds ranges or something else, but everything in the code above should be fine.
I'm thinking the issue stems after the 'Sort' part begins, but no matter what I change or remove, I can't get it to sort the "A" column.
keyfrom the active sheet. It will work as expected, only if the "Sheet1" is the active one... It should be good to declare aWorksheetvariable, let us say,ws, setting it as the sheet you need and using it asws.Range("A2:BT9999").Sort Key1:=ws.Range("A:A"), Order1:=xlAscending