TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 250
- Office Version
- 365
- 2021
- Platform
- Windows
Looking for help on this problem which has stumped me today!
The basic purpose of my code is:
Here is a picture of typical source data, with the duplicates highlighted:
The check column is column B - entry number.
What I need to happen is:
Note that the 2 sets of duplicates now occupy 1 line, and the total score 2 has been added up. The other fields all match. (There is more in the real data).
There are 19 line numbers.
But for Output Array 2, I need something slightly different:
I tried the following code:
But the problems I encountered were:
In the Output 2 page, the data is correct but the line numbering is all wrong:
So - where do I go from here!
Note that the real data has about 26 columns and thousands of lines so I'm trying to avoid too many loops.
The basic purpose of my code is:
- Load the data from a range into an array in VBA
- Play with that data a bit
- Put the outputs from that manipulation into 2 new arrays in VBA
- Output the arrays to 2 different ranges in the original workbook
Here is a picture of typical source data, with the duplicates highlighted:
The check column is column B - entry number.
What I need to happen is:
- When looping through my array, find out if the entry in position LoopCounter,2 matches the entry in LoopCounter -1,2 - i.e. is it the same as the last line
- If it is, for Output Array 1, simply add the value of Score 2 cell to the value already existing in the Score 2 for the previous row, i.e. a total score for all lines with that entry number
Note that the 2 sets of duplicates now occupy 1 line, and the total score 2 has been added up. The other fields all match. (There is more in the real data).
There are 19 line numbers.
But for Output Array 2, I need something slightly different:
- Each line from the source is shown as an individual line with its own row in the output
- Each individual element of the duplicated lines is shown by its own
- But each line of the duplicate shares the same line number as that row in Output Array 1.
- So there will be 22 rows of data, but only 19 line numbers matching Output Array 1
I tried the following code:
VBA Code:
Sub PopulateArrays()
Set Wbk1 = ThisWorkbook
Set WS1 = Wbk1.Sheets("Source Data")
Set WS2 = Wbk1.Sheets("Output 1")
Set WS3 = Wbk1.Sheets("Output 2")
Dim LoadArray As Variant, Output1 As Variant, Output2 As Variant
Dim Counter As Long, RowNumber As Long
LoadArray = WS1.Range("A2:F23")
ReDim Output1(1 To 23, 1 To 4) As Variant
ReDim Output2(1 To 23, 1 To 3) As Variant
RowNumber = 1
For Counter = 1 To UBound(LoadArray)
If Counter = 1 Then GoTo FullLoad
If LoadArray(Counter, 2) = LoadArray(Counter - 1, 2) Then
Output1(Counter - 1, 4) = Output1(Counter - 1, 4) + LoadArray(Counter, 6)
Output2(Counter, 2) = LoadArray(Counter, 5)
Output2(Counter, 3) = LoadArray(Counter, 6)
Output2(Counter, 1) = Counter - 1
GoTo Skip
End If
FullLoad:
Output1(Counter, 4) = LoadArray(Counter, 6)
Output1(Counter, 1) = Counter
Output1(Counter, 2) = LoadArray(Counter, 3)
Output1(Counter, 3) = LoadArray(Counter, 1)
Output2(Counter, 1) = Counter
Output2(Counter, 2) = LoadArray(Counter, 5)
Output2(Counter, 3) = LoadArray(Counter, 6)
Skip:
Next Counter
WS2.Range("A2").Resize(UBound(Output1), UBound(Output1, 2)) = Output1
WS3.Range("A2").Resize(UBound(Output2), UBound(Output2, 2)) = Output2
End Sub
But the problems I encountered were:
- It kind of worked for the duplicate, but I now have empty lines where the ignored line is and the line numbering has gone wrong
- It got the reference that was duplicated completely wrong
In the Output 2 page, the data is correct but the line numbering is all wrong:
So - where do I go from here!
Note that the real data has about 26 columns and thousands of lines so I'm trying to avoid too many loops.