Hello,
I am working on a project that will have a variable range of rows that I will need to sort the data and format the entire range. Columns B-G will always have data in them, but I need to be able to sort columns A-I, regardless of data included in columns A, H, & I.
Now, using the macro recorder I have been able to do it for a fixed range, but I will need to make this work for anything between 25 and 5000 rows depending on the day.
Essentially, I need to find the last row of column G, select the cell in column I of that row, and then select the range of a4:I, apply all borders, and sort by columns E then B.
Here is the fixed range code where the last data point is in G27:
I for the life of me cannot get a dynamic range to select beyond row 4 in my sample set. Any thoughts would be greatly appreciated. I feel like I might be over complicating what needs to be done, but I can't see where.
Thanks,
Mike
I am working on a project that will have a variable range of rows that I will need to sort the data and format the entire range. Columns B-G will always have data in them, but I need to be able to sort columns A-I, regardless of data included in columns A, H, & I.
Now, using the macro recorder I have been able to do it for a fixed range, but I will need to make this work for anything between 25 and 5000 rows depending on the day.
Essentially, I need to find the last row of column G, select the cell in column I of that row, and then select the range of a4:I, apply all borders, and sort by columns E then B.
Here is the fixed range code where the last data point is in G27:
Code:
Sub Test()
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
Range("I27").Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("E4:E27") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B4:B27") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A3:I27")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I for the life of me cannot get a dynamic range to select beyond row 4 in my sample set. Any thoughts would be greatly appreciated. I feel like I might be over complicating what needs to be done, but I can't see where.
Thanks,
Mike