Refine VBA code to summarise by max values

Peter1986

New Member
Joined
Jan 22, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
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).

Example_SummaryTable.JPG


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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this:
VBA Code:
Sub a1122767a()
'https://www.mrexcel.com/board/threads/refine-vba-code-to-summarise-by-max-values.1122754/
Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, vc

n = Range("A" & Rows.Count).End(xlUp).Row
va = Range("B1:B" & n)
vb = Range("A1:B" & n)
ReDim vc(1 To n, 1 To 4)

For i = 1 To n
    If va(i, 1) <> 0 Then va(i, 1) = 1
Next

For i = 3 To UBound(va, 1)
j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
       
        i = i - 1
        k = k + 1
        vc(k, 1) = vb(j, 1)
        vc(k, 2) = vb(i, 1)
        vc(k, 3) = WorksheetFunction.Max(Cells(j, "B"), Cells(i, "B"))
        vc(k, 4) = i - j + 1

Next

Range("E3").Resize(k, 4) = vc

End Sub

Book1
ABCDEFGH
1
2
3101303
4204754
53081003
641111272
752131301
861141532
975
1080
1190
12100
13111
14127
15130
16143
17153
Sheet1
 
Upvote 0
Try this:
VBA Code:
Sub a1122767a()
'https://www.mrexcel.com/board/threads/refine-vba-code-to-summarise-by-max-values.1122754/
Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, vc

n = Range("A" & Rows.Count).End(xlUp).Row
va = Range("B1:B" & n)
vb = Range("A1:B" & n)
ReDim vc(1 To n, 1 To 4)

For i = 1 To n
    If va(i, 1) <> 0 Then va(i, 1) = 1
Next

For i = 3 To UBound(va, 1)
j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
      
        i = i - 1
        k = k + 1
        vc(k, 1) = vb(j, 1)
        vc(k, 2) = vb(i, 1)
        vc(k, 3) = WorksheetFunction.Max(Cells(j, "B"), Cells(i, "B"))
        vc(k, 4) = i - j + 1

Next

Range("E3").Resize(k, 4) = vc

End Sub

Book1
ABCDEFGH
1
2
3101303
4204754
53081003
641111272
752131301
861141532
975
1080
1190
12100
13111
14127
15130
16143
17153
Sheet1

Akuini,

Thank you once again this code works very efficiently.

Note I have made one minor edit changing the following:
vc(k, 3) = WorksheetFunction.Max(Cells(j, "B"), Cells(i, "B"))
to:
Set myRange = Range((Cells(j, "B")), (Cells(i, "B")))
vc(k, 3) = WorksheetFunction.Max(myRange)

so we capture the max of the range rather than the max of the first and last entry.

Thank you!
 
Upvote 0
Ah, sorry my mistake.
Glad you figured it out. A simpler one is:
vc(k, 3) = WorksheetFunction.Max(Range(Cells(j, "B"), Cells(i, "B")))
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top