Hi
Take for example ABC10 in Cell A1, ABC199 in cell A2 and ABC9 in cell A3
How am i able to sort such that ABC9 appears in cell A1 followed by ABC10 in cell A2 and ABC199 in cell A3?
VBA advice would be appreciated.
Thanks.
Hi
Take for example ABC10 in Cell A1, ABC199 in cell A2 and ABC9 in cell A3
How am i able to sort such that ABC9 appears in cell A1 followed by ABC10 in cell A2 and ABC199 in cell A3?
VBA advice would be appreciated.
Thanks.
Re: Sorting Numbers Before Alphabets Within A Cell
Use the Custom Function here called Extract Numbers From AlphaNumerics In another Column. Then Copy Edit>Paste Special - Values and sort both columns by the numeric column.
Re: Sort AlphaNumeric Text By Numbers
Thanks for the swift reply.
However, Due to circumstances, I am not allowed to create an additional column. Would there be a macro to do all this together?
Cheers
Re: Sort AlphaNumeric Text By Numbers
dtwk,
Is it always 3 letters then numbers?
dr
Re: Sort AlphaNumeric Text By Numbers
Yes dr.
Its the same ABC followed by a set of numbers.
Cheers
Re: Sort AlphaNumeric Text By Numbers
Dave
ABC10 will still come before ABC9 even thought 9 < 10... =(
Re: Sort AlphaNumeric Text By Numbers
How about pasting the column to a new worksheet, taking off the "ABC", sorting, putting the ABC back in, and pasting back to the original sheet?
Re: Sort AlphaNumeric Text By Numbers
Hi DTWK -
Clarify again -
are the first 3 letters ALWAYS "ABC"?
are the numbers ALWAYS 3 digits?
would it be acceptable to change the numbers into 3 digit numbers where by ABC9 would become ABC009
If all of the above are true, its not that difficult to write VBA, if either rule is broken, it becomes a little more complicated to write a generic routine.
Ger
Re: Sort AlphaNumeric Text By Numbers
Use this code
Sub SortAlphaNumerics()
Dim wSheetTemp As Worksheet
Dim wsStart As Worksheet
Dim lLastRow As Long
''''''''''''''''''''''''''''''''''''''''''
'Written by OzGrid Business Applications
'www.ozgrid.com
'Sorts Alphanumerics Of Column "A" of active Sheet.
'ExtractNumber Function REQUIRED
'http://www.ozgrid.com/VBA/ExtractNum.htm
''''''''''''''''''''''''''''''''''''''''''
Application.ScreenUpdating = False
Set wsStart = ActiveSheet
Set wSheetTemp = Worksheets.Add
With wsStart
lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
.Range("A1:A" & lLastRow).Copy _
wSheetTemp.Range("A1")
End With
With wSheetTemp.Range("B1:B" & lLastRow)
.FormulaR1C1 = "=ExtractNumber(RC[-1])"
.Copy
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
With wSheetTemp.UsedRange
.Sort Key1:=.Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
.Columns(1).Cut wsStart.Range("A1")
End With
Application.DisplayAlerts = False
wSheetTemp.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Function ExtractNumber(rCell As Range) As Double
Dim iCount As Integer, i As Integer
Dim sText As String
Dim lNum As String
Dim vVal
''''''''''''''''''''''''''''''''''''''''''
'Written by OzGrid Business Applications
'www.ozgrid.com
'Extracts a number from a cell containing text and numbers.
''''''''''''''''''''''''''''''''''''''''''
sText = rCell
For iCount = Len(sText) To 1 Step -1
vVal = Mid(sText, iCount, 1)
If IsNumeric(vVal) Or vVal = "." Or vVal = "-" Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
End If
If i = 1 Then lNum = CDbl(Mid(lNum, 1, 1))
Next iCount
ExtractNumber = CDbl(lNum)
End Function
Display More
Re: Sort AlphaNumeric Text By Numbers
or maybe just something simple like
Sub sortwithletters()
With Range("a1", Range("a1").End(xlDown))
.Replace What:="ABC", Replacement:="99999999", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Replace What:="99999999", Replacement:="ABC", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End With
End Sub
Re: Sort AlphaNumeric Text By Numbers
nice idea....
Re: Sort AlphaNumeric Text By Numbers
dtwk,
Another way. Uses blank rows on the sheet to split and sort.
This version is for a single column of data
Sub sortAlphaNumeric()
Dim i As Long 'Loop counter
Dim lrow As Long 'Last row of data
'Presumes sorting 1 column only
'Presumes Col as maximum extents of data
Application.ScreenUpdating = False
'Get last row of data Col A (+1 for first blank row)
lrow = Range("A65536").End(xlUp).Row + 1
'Variable for compare
nrow = lrow
'Use empty space as storage, splitting cell values
'Loop on all rows
For i = 1 To lrow - 1
'Get alpha
Cells(nrow, 1) = Left(Cells(i, 1), 3)
'Get numeric
Cells(nrow, 2) = Right(Cells(i, 1), Len(Cells(i, 1)) - 3)
'Increment row
nrow = nrow + 1
Next i
'Sort by numeric then alpha
Range(Cells(lrow, 1), Cells(nrow - 1, 2)).Select
Selection.Sort _
Key1:=Range("B" & lrow), Order1:=xlAscending, _
Key2:=Range("A" & lrow), Order2:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom
'Copy/Paste back
'where to work
nrow = lrow
'Loop on all cells
For i = 1 To lrow - 1
'Rebuild original values
Cells(i, 1) = Cells(nrow, 1) & Cells(nrow, 2)
'Increment row
nrow = nrow + 1
Next i
'Delete temp(working)range
Range(Cells(lrow, 1), Cells(nrow, 2)).ClearContents
End Sub
Display More
This version works on multiple columns of data
Sub sortAlphaNumericMulti()
Dim i As Long 'Loop counter
Dim lrow As Long 'Last row of data
Dim nrow As Long 'New lastrow after copy
Dim lcol As Long 'Last col of data, this row
'Presumes data in Col A and Row 1 has maximum extents
'Get last row of data Col A (+1 for first blank row)
lrow = Range("A65536").End(xlUp).Row + 1
'Variable for compare
nrow = lrow
'Get last column of data (row 1)
lcol = Range("IV1").End(xlToLeft).Column
'Use empty space as storage, splitting cell values
'Loop on all rows
For i = 1 To lrow - 1
'Get alpha
Cells(nrow, 1) = Left(Cells(i, 1), 3)
'Get numeric
Cells(nrow, 2) = Right(Cells(i, 1), Len(Cells(i, 1)) - 3)
'Copy rest of data
Range(Cells(i, 2), Cells(i, lcol)).Copy Cells(nrow, 3)
'Increment row
nrow = nrow + 1
Next i
'Sort by numeric then alpha
Range(Cells(lrow, 1), Cells(nrow - 1, lcol + 1)).Select
Selection.Sort _
Key1:=Range("B" & lrow), Order1:=xlAscending, _
Key2:=Range("A" & lrow), Order2:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom
'Copy/Paste back
'where to work
nrow = lrow
'Loop on all cells
For i = 1 To lrow - 1
'Rebuild original values
Cells(i, 1) = Cells(nrow, 1) & Cells(nrow, 2)
'Copy sorted
Range(Cells(nrow, 3), Cells(nrow, lcol + 1)).Copy Cells(i, 2)
'Increment row
nrow = nrow + 1
Next i
'Delete temp(working)range
Range(Cells(lrow, 1), Cells(nrow - 1, lcol + 1)).ClearContents
Application.ScreenUpdating = True
End Sub
Display More
Cheers,
dr
Re: Sort AlphaNumeric Text By Numbers
Quote from Badger101or maybe just something simple like
CodeSub sortwithletters() With Range("a1", Range("a1").End(xlDown)) .Replace What:="ABC", Replacement:="99999999", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False .Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom .Replace What:="99999999", Replacement:="ABC", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False End With End Sub
DOH!
Re: Sort AlphaNumeric Text By Numbers
Nice Badger... It works perfectly... Thanks a lot to all u guys... been a great help since i registered to this website... Thanks to Dave and all... /bow /salute
Don’t have an account yet? Register yourself now and be a part of our community!