tyija1995
Well-known Member
- Joined
- Feb 26, 2019
- Messages
- 781
- Office Version
- 365
- Platform
- Windows
Hi all,
I have made a macro at work to basically calculate the maximum length (of a cell) for each column in a table with header row 1 (used for SQL truncation purposes... I.e. "Job Title" may have type nvarchar(20))
The macro works all well and good but it can be pretty slow when dealing with high quantities of data, so I was wondering if there were any improvements I can make to the code?
Here is a very short example:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Job Title[/TD]
[TD]Description[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Cleaner[/TD]
[TD]Cleans the office[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Data Analyst[/TD]
[TD]Analyses data in SQL databases[/TD]
[/TR]
</tbody>[/TABLE]
If I execute the macro on this table of data above I get:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Job Title[/TD]
[TD]Description[/TD]
[TD][/TD]
[TD]Field Name[/TD]
[TD]Max Length[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Cleaner[/TD]
[TD]Cleans the office[/TD]
[TD][/TD]
[TD]Job Title[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Data Analyst[/TD]
[TD]Analyses data in SQL databases[/TD]
[TD][/TD]
[TD]Description[/TD]
[TD]30[/TD]
[/TR]
</tbody>[/TABLE]
So my question really is how can I improve on the code I have written for speed purposes?
I hope this makes sense, as I say it works fine but it is quite slow with larger tables of data.
Thanks for any responses!
I have made a macro at work to basically calculate the maximum length (of a cell) for each column in a table with header row 1 (used for SQL truncation purposes... I.e. "Job Title" may have type nvarchar(20))
The macro works all well and good but it can be pretty slow when dealing with high quantities of data, so I was wondering if there were any improvements I can make to the code?
Here is a very short example:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Job Title[/TD]
[TD]Description[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Cleaner[/TD]
[TD]Cleans the office[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Data Analyst[/TD]
[TD]Analyses data in SQL databases[/TD]
[/TR]
</tbody>[/TABLE]
If I execute the macro on this table of data above I get:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Job Title[/TD]
[TD]Description[/TD]
[TD][/TD]
[TD]Field Name[/TD]
[TD]Max Length[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Cleaner[/TD]
[TD]Cleans the office[/TD]
[TD][/TD]
[TD]Job Title[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Data Analyst[/TD]
[TD]Analyses data in SQL databases[/TD]
[TD][/TD]
[TD]Description[/TD]
[TD]30[/TD]
[/TR]
</tbody>[/TABLE]
So my question really is how can I improve on the code I have written for speed purposes?
Code:
Sub MaxLength()
Application.ScreenUpdating = False
Dim i As Long, j As Long, lastRow As Long, lastCol As Long, maxLen As Long, lastDelRow As Long
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
On Error Resume Next
For j = 1 To lastCol
lastRow = Cells(Rows.Count, j).End(xlUp).Row
For i = 2 To lastRow
Cells(i, lastCol + 1) = Len(Cells(i, j))
Next i
maxLen = WorksheetFunction.Max(Range(Cells(2, lastCol + 1), Cells(lastRow, lastCol + 1)))
Cells(1, lastCol + 2).Value = "Field Name"
Cells(1, lastCol + 3).Value = "Max Length"
Cells(j + 1, lastCol + 2).Value = Cells(1, j)
Cells(j + 1, lastCol + 3).Value = maxLen
Next j
lastDelRow = Cells(Rows.Count, lastCol + 1).End(xlUp).Row
Range(Cells(2, lastCol + 1), Cells(lastDelRow, lastCol + 1)).Clear
Range(Cells(1, lastCol + 2), Cells(Cells(Rows.Count, lastCol + 2).End(xlUp).Row, lastCol + 3)).Columns.AutoFit
Range(Cells(1, lastCol + 2), Cells(Cells(Rows.Count, lastCol + 2).End(xlUp).Row, lastCol + 3)).Select
Application.ScreenUpdating = True
End Sub
I hope this makes sense, as I say it works fine but it is quite slow with larger tables of data.
Thanks for any responses!