rjplante
Well-known Member
- Joined
- Oct 31, 2008
- Messages
- 574
- Office Version
- 365
- Platform
- Windows
I have the macro below which is used to sort a block of numbers and some numbers with letters:
306
306A
306D
308
The request has now been made to have a preceding letter. I need to adjust the macro so that it sorts the numbers in the following order. The preceding number will always be an N.
306
306A
306D
N306
308
I do not know how to adjust this macro as I obtained the macro here from one of the wizards who know VBA way better than I do. I don't fully understand how it works. If anyone can help me adjsut this so that it sorts the numbers like I have listed above, that would be great.
Thanks for the help.
306
306A
306D
308
The request has now been made to have a preceding letter. I need to adjust the macro so that it sorts the numbers in the following order. The preceding number will always be an N.
306
306A
306D
N306
308
I do not know how to adjust this macro as I obtained the macro here from one of the wizards who know VBA way better than I do. I don't fully understand how it works. If anyone can help me adjsut this so that it sorts the numbers like I have listed above, that would be great.
Thanks for the help.
VBA Code:
Option Explicit
Dim numbers As New Collection
Sub SortNumbersWithLetters()
Dim C As Range
Dim a As Variant, n As Variant, m As Variant, x As Variant
Dim i As Long, j As Long, k As Long
Dim col As Long
Set numbers = Nothing
col = Sheets("Block Tracking All").Range("CF1").Value + 1
a = Range("A5", Cells(Range("A" & Rows.Count).End(3).Row, col)).Value
ReDim b(1 To UBound(a), 1 To col)
For i = 1 To UBound(a)
n = Val(a(i, 1))
m = Mid(a(i, 1), Len(n) + 1)
addnum Format(n, "000000000") & m
Next
i = 1
For Each x In numbers
n = Val(x)
m = Mid(x, 10)
b(i, 1) = Val(n) & m
For j = 1 To UBound(a)
If "" & a(j, 1) = b(i, 1) Then
For k = 2 To UBound(a, 2)
b(i, k) = a(j, k)
Next
Exit For
End If
Next
i = i + 1
Next
Range("A5").Resize(UBound(b, 1), UBound(b, 2)).Value = b
Call FC_CLEAR
End Sub
Sub addnum(C)
Dim ii As Long
For ii = 1 To numbers.Count
Select Case StrComp(numbers(ii), C, vbTextCompare)
Case 0, 1
numbers.Add C, Before:=ii
Exit Sub
End Select
Next
numbers.Add C 'add to end
End Sub