Gingertrees
Well-known Member
- Joined
- Sep 21, 2009
- Messages
- 697
Found some code a Contextures for locating the LAstRow and LastColumn in a worksheet. Trying to combine it with a macro to sort my data. I am trying to modify the macro-recorder's data to allow for the variable end of a worksheet (I recorded it such that it sorted from A1:Q205).
I come up with two errors here. First, syntax error at any reference to LastRow - which I seem to have defined as the address of the last CELL... though I don't know how, reading the code.
Second, it doesn't actually sort, even when I can get the code to run. I want it to sort by column K first, then column E (Timezone and State, respectively).
Help please? Here's my code:
Here's some sample data that is SHOULD be sorting:
I come up with two errors here. First, syntax error at any reference to LastRow - which I seem to have defined as the address of the last CELL... though I don't know how, reading the code.
Second, it doesn't actually sort, even when I can get the code to run. I want it to sort by column K first, then column E (Timezone and State, respectively).
Help please? Here's my code:
Code:
Sub Formatter()
Application.ScreenUpdating = FalseApplication.Calculation = xlCalculationManual
Dim LastColumn As Integer
Dim LastRow As Long
Dim LastCell As Range
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Search for any entry, by searching backwards by Columns.
LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
'MsgBox Cells(LastRow, LastColumn).Address
' LastCell = Cells.Address(LastRow, LastColumn)
End If
Cells.Select
Selection.ClearFormats
Columns("O:O").Select
Selection.NumberFormat = "m/d/yyyy"
Cells.Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("K2:K" & LastRow & ") _ '/////syntax error
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=Range("E2:E" & LastRow & ") _ '/////syntax error
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.Sort
.SetRange ("A1:Q" & LastRow)'///syntax error
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'///other stuff
End Sub
Here's some sample data that is SHOULD be sorting:
Code:
PHONE,NAME,ADDRESS,CITY,STATE,ZIP,SERIAL,TYPE,ID1,ID2,TIMEZONE,OTHER
5551112222,JAmes,300 Any St,Culver, GA,30016,2344,A,3423,3434,Eastern,gt43
8881112223,Amy,344 A Ave,Anthem,AZ,85086,4813,B,e34,345,Mtn,345
7775465656,Robert,72 Main ST,Anytown,MD,21102,3532,A,5463,Eastern,46j2
8002223221,Janet,900 Oak St,Laramie,WY,82070,843as,B,3453,5642,Mtn,342jh
5552143251,Jeannie,6464 Manchester RD,Daphne,AL,36526,A,343534,746t,Central,43535s
6541234561,David,4 B st,Charlottesville,VA,22901,B,3424,4543,Eastern,345jh