Hello,
I'm creating an excel template with a formatting macro. The template will have data of varying length added to it (could be 10 rows, could be 1000). I created a macro using Record Macro, then added a section to add a border after each unique value in the last column. It's working great (see VBA code below), except that the sort columns section of the code is specifying row 357 as the end of the range (the last row of my test data). When I tried using the macro on 400 rows, the sorting restarted after 357.
I tried replacing this:
Key:=Range("T2:T357" _
),
With this:
Key:=Range("T2:T" & LastRow)
Or this:
Key:=Range("T:T")
But when I do that I get an error message that highlights "LastRow as Long" in the next command and gives me this error: Compile error: Duplicate declaration in current scope
Thank you for your help!!!
I'm creating an excel template with a formatting macro. The template will have data of varying length added to it (could be 10 rows, could be 1000). I created a macro using Record Macro, then added a section to add a border after each unique value in the last column. It's working great (see VBA code below), except that the sort columns section of the code is specifying row 357 as the end of the range (the last row of my test data). When I tried using the macro on 400 rows, the sorting restarted after 357.
I tried replacing this:
Key:=Range("T2:T357" _
),
With this:
Key:=Range("T2:T" & LastRow)
Or this:
Key:=Range("T:T")
But when I do that I get an error message that highlights "LastRow as Long" in the next command and gives me this error: Compile error: Duplicate declaration in current scope
VBA Code:
Cells.Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("T2:T357" _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("B2:B357" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:AV357")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Dim LastRow As Long
Dim xrg As Range
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For Each xrg In Range("AV2:AV" & LastRow)
If xrg <> xrg.Offset(1, 0) Then
Range("A" & xrg.Row & ":AV" & xrg.Row).Borders(xlEdgeBottom).LineStyle = xlContinuous
End If
Next xrg
End Sub
Thank you for your help!!!