![]() |
How to sort number follow cell accordingly
Hi, everyone!
I need to create macro that can sort numbers as below: Column : Before A B C D E F 1 2 3 4 5 6 3 4 5 1 3 4 5 3 5 6 After A B C D E F 1 2 3 4 5 6 3 4 5 1 3 4 5 3 5 6 which mean same number follow the same column but not necessary follow column A,B,C..... Pls help with vba code.Thanks |
How to sort number follow cell accordingly
GeniusIdeas,
You have to evaluate the assumptions in the program (i.e. read the comments and evaluate the code). Basically, Integers are assumed for the sort, and the Integers start at 1 and each consecutive value is 1 plus the previous value. If you are not familiar with the MATCH function, look it up with the Excel help. Best, Matthew Herbert Sub CustomSortRoutine() Dim Rng As Range Dim rngCell As Range Dim rngTemp As Range Dim intArrHdr() As Integer Dim varArr As Variant Dim varItem As Variant Dim intCnt As Integer Dim lngCnt As Long Dim varMatch As Variant Application.ScreenUpdating = False 'assumes you are using Integers only Set Rng = Selection 'create a lookup array and fill it will consecutive ' numbers, starting at 1; assumes Option Base 0 ' which is the default setting ReDim intArrHdr(Rng.Columns.Count - 1) For intCnt = LBound(intArrHdr) To UBound(intArrHdr) intArrHdr(intCnt) = intCnt + 1 Next intCnt 'loop through each row of Rng For lngCnt = 1 To Rng.Rows.Count 'create a temporary range that references the ' entire row Set rngTemp = Rng.Rows(lngCnt) 'load the current row's values into an array varArr = rngTemp.Value 'clear the temporary range rngTemp.Clear 'loop through each item in the array For Each varItem In varArr 'match the varItem with intArrHdr. If a match exists, ' offset the value to the appropriate position in the ' row. (If no match, do nothing). varMatch = Application.Match(varItem, intArrHdr, 0) If Not IsError(varMatch) Then 'offset from the far-left cell of the row, placing ' the matching value into the offset cell rngTemp.Cells(1, 1).Offset(0, varMatch - 1).Value = varItem End If Next varItem Next lngCnt End Sub "geniusideas" wrote: Hi, everyone! I need to create macro that can sort numbers as below: Column : Before A B C D E F 1 2 3 4 5 6 3 4 5 1 3 4 5 3 5 6 After A B C D E F 1 2 3 4 5 6 3 4 5 1 3 4 5 3 5 6 which mean same number follow the same column but not necessary follow column A,B,C..... Pls help with vba code.Thanks . |
How to sort number follow cell accordingly
Thank matthew..Its work fine
|
All times are GMT +1. The time now is 10:39 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com