antman2988
Board Regular
- Joined
- Jun 28, 2018
- Messages
- 78
Hello! I'm using the below code to reorder my worksheet's columns based on a predetermined column order. This code works as long as there are less than 500k rows, but once the number of rows exceeds 500k, I receive an application-defined or object-defined error. Is this code limited by memory? If so, is there a better way to write this code? I saw this post and was wondering if it would be a better solution. Also, in my code, I create a table object and was wondering if I should reorder the ListColumns instead. By the time the table object has been created, there are quite a few less columns, which would probably help with the memory issues. What's a good way to reorder ListColumns? Is it possible? I wasn't able to find much on it so far.
The commented array code is what I used to rearrange the columns after the table object was created, but it kept destroying the table so I decided to rearrange the columns before creating the table, but now I'm running into issues with larger files. Also, what would the one line code look like if it were to be broken out?
Any help is appreciated. Thanks!
VBA Code:
'This macro will reorder the column numbers by declaring the new order in an array
'It will then use the Evaluate and Index functions to place the range's existing column order in the new order
Sub RearrangeColumns()
Dim newColumnOrder As Variant
'newColumnOrder = Array(1, 2, 3, 4, 41, 42, 43, 44, 5, 6, 49, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 47, 48, 50, 51, 52)
newColumnOrder = Array(1, 2, 3, 4, 78, 79, 80, 81, 5, 6, 86, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, _
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 82, 83, 84, 85, 87, 88, 89, 90)
Range("A1").Resize(Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row, UBound(newColumnOrder) + 1) = Application.Index(Cells, Evaluate("ROW(1:" & Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row & ")"), newColumnOrder)
End Sub
The commented array code is what I used to rearrange the columns after the table object was created, but it kept destroying the table so I decided to rearrange the columns before creating the table, but now I'm running into issues with larger files. Also, what would the one line code look like if it were to be broken out?
Any help is appreciated. Thanks!