Hello there!
I have an existing code which creates a summary table from a dataset by counting the number of repeated consecutive values. If there is a new value in the dataset then a new row is created for it in the summary table.
Please can you advise if it’s possible to edit this macro (or otherwise re-write it) so that it counts all the positive consecutive values (whether they are the same or not) and then adds this to the results table noting down the MAX value in the set. I have included an image below to clarify what I am trying to achieve (existing output in black, required output in red).
This macro will ultimately be used to sort a very large dataset. Any help is very much appreciated!
The existing code is as follows (however I am happy to replace this if there is a better method):
I have an existing code which creates a summary table from a dataset by counting the number of repeated consecutive values. If there is a new value in the dataset then a new row is created for it in the summary table.
Please can you advise if it’s possible to edit this macro (or otherwise re-write it) so that it counts all the positive consecutive values (whether they are the same or not) and then adds this to the results table noting down the MAX value in the set. I have included an image below to clarify what I am trying to achieve (existing output in black, required output in red).
This macro will ultimately be used to sort a very large dataset. Any help is very much appreciated!
The existing code is as follows (however I am happy to replace this if there is a better method):
VBA Code:
Sub Summarise_Data()
Dim sht As Worksheet, rngSource As Range, rngDest As Range
Dim arrNumbers(3) As Double
Set sht = Worksheets("Sheet1")
Set rngSource = sht.Range("A3")
Set rngDest = sht.Range("E3:H3")
arrNumbers(0) = rngSource.Value
arrNumbers(1) = rngSource.Value
arrNumbers(2) = rngSource.Offset(0, 1).Value
arrNumbers(3) = 1
Set rngSource = rngSource.Offset(1, 0)
Do Until rngSource.Value = ""
If rngSource.Offset(0, 1).Value <> arrNumbers(2) Then
rngDest.Value = arrNumbers
Set rngDest = rngDest.Offset(1, 0)
arrNumbers(0) = rngSource.Value
arrNumbers(1) = rngSource.Value
arrNumbers(2) = rngSource.Offset(0, 1).Value
arrNumbers(3) = 1
Else
arrNumbers(1) = rngSource.Value
arrNumbers(3) = arrNumbers(3) + 1
End If
Set rngSource = rngSource.Offset(1, 0)
Loop
rngDest.Value = arrNumbers
End Sub