OaklandJim
Well-known Member
- Joined
- Nov 29, 2018
- Messages
- 855
- Office Version
- 365
- Platform
- Windows
I am trying to assist another list user with a pretty arcane need. Thanx a billion for assisting.
I have the main infrastructure of the workbook built but I have not been able to get the logic right for the sort of an array. My array has three values for each entry. They are: 1. the digit being processed, 2. the first occurrence of the digit in a specified range of values and 3. the second occurrence of the digit in the same specified range of values. There are three digits to process so the array is 3 x 3. The sort on the array is done using the second value in the entries (the first occurrence of the digit in a specified range of values). All values are integer (type long).
Here is the workbook with a call to the function in Sheet2: WORKBOOK
Below is the function that I am trying to develop. It is the module named Func_GetOrderOfAppearance. It is working up to the point of the sort. I normally write "fluffy" code with lots of comments so 1. if I have to use the code later it'll be understandable and/or 2. if I give it to a list user I want her to be able to understand the code. This is no exception.
I have the main infrastructure of the workbook built but I have not been able to get the logic right for the sort of an array. My array has three values for each entry. They are: 1. the digit being processed, 2. the first occurrence of the digit in a specified range of values and 3. the second occurrence of the digit in the same specified range of values. There are three digits to process so the array is 3 x 3. The sort on the array is done using the second value in the entries (the first occurrence of the digit in a specified range of values). All values are integer (type long).
Here is the workbook with a call to the function in Sheet2: WORKBOOK
Below is the function that I am trying to develop. It is the module named Func_GetOrderOfAppearance. It is working up to the point of the sort. I normally write "fluffy" code with lots of comments so 1. if I have to use the code later it'll be understandable and/or 2. if I give it to a list user I want her to be able to understand the code. This is no exception.
VBA Code:
Option Explicit
Function GetOrderOfAppearance( _
ByVal prRangeWithDigits As Range, _
ByVal prRangeToLookIn As Range) As Long
' Count of columns containing digits.
Dim iDigitsCount As Long
' Used to iterate through the values in the aiRaw array.
Dim iDigitLoop As Long
' Used to iterate through the values in the aiRaw array to sort it.
Dim iDigitLoopInner As Long
' The digit being processed.
Dim iDigitValue As Long
' The first occurrence of the digit within the "look in" data range.
Dim iFirstAppearance As Long
' The second occurrence of the digit within the "look in" data range.
Dim iSecondAppearance As Long
GetOrderOfAppearance = 0
' Count of columns in the data to process.
iDigitsCount = prRangeWithDigits.Columns.Count
' Do sizing for the array containing raw data directly from the "look in" data (range)
ReDim aiRaw(1 To iDigitsCount, iDigitsCount)
' Do sizing for the array containing sorted data in the aiRaw array.
ReDim aiInOrder(1 To iDigitsCount, iDigitsCount)
' Load the aiRaw array with 1. the digit 2. the first occurrence of the
' digit in the "look in" data (range) and 3. the second occurrence of
' the digit in the "look in" data (range)
For iDigitLoop = 1 To iDigitsCount
' Get the digit to be processed.
iDigitValue = prRangeWithDigits.Cells(1, iDigitLoop).Value
' Load the digit being processed into the array
aiRaw(1, iDigitLoop) = iDigitValue
' Load the first occurence of the digit in the "look in" data range
aiRaw(2, iDigitLoop) = FindValueFirstOccurrence(iDigitValue, prRangeToLookIn)
' Load the second occurence of the digit in the "look in" data range
aiRaw(3, iDigitLoop) = FindValueSecondOccurrence(iDigitValue, prRangeToLookIn)
Next
' For iDigitLoop = 1 To iDigitsCount
'
' Debug.Print
' Debug.Print aiRaw(1, iDigitLoop)
' Debug.Print aiRaw(2, iDigitLoop)
' Debug.Print aiRaw(3, iDigitLoop)
''
' Next iDigitLoop
'
For iDigitLoop = 1 To iDigitsCount
iDigitValue = aiRaw(1, iDigitLoop)
iFirstAppearance = aiRaw(2, iDigitLoop)
iSecondAppearance = aiRaw(3, iDigitLoop)
For iDigitLoopInner = 1 To iDigitsCount
If iFirstAppearance >= aiRaw(2, iDigitLoopInner) _
Then
If iDigitLoopInner < iDigitsCount _
Then
aiInOrder(1, iDigitLoopInner + 1) = aiRaw(1, iDigitLoopInner)
aiInOrder(2, iDigitLoopInner + 1) = aiRaw(2, iDigitLoopInner)
aiInOrder(3, iDigitLoopInner + 1) = aiRaw(3, iDigitLoopInner)
End If
aiInOrder(1, iDigitLoopInner) = iDigitValue
aiInOrder(2, iDigitLoopInner) = iFirstAppearance
aiInOrder(3, iDigitLoopInner) = iSecondAppearance
End If
Next
'
Next iDigitLoop
' For iDigitLoop = 1 To iDigitsCount
'
' Debug.Print
' Debug.Print aiInOrder(1, iDigitLoop)
' Debug.Print aiInOrder(2, iDigitLoop)
' Debug.Print aiInOrder(3, iDigitLoop)
''
' Next iDigitLoop
End Function