How to find the maximum consecutive repeated value on the bases of two column in VBA , Excel

susheeltyagi

New Member
Joined
Jan 5, 2022
Messages
4
Platform
  1. Windows
  2. Mobile
  3. Web
Hi,
I need the expert help in VBA as I am new. Actually I am looking for Vba code for Consecutive Count on the bases of two column (Serial Number and Alert Code) on button click event. The Column row are not fixed (dynamically change). The Consecutive count is maximum repeat count for Alert Code per Serial number. This should display in output worksheet as per max repeat Alert count per Serial number


Input Worksheet:

image1.jpg


Expected Output :

image2.jpg


The repeat count work as below pattern from Input sheet (Just for reference only).

image3.jpg
 
Thanks for looking at it. I should probably leave it there since it is not really relevant to the OPs thread.
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Does this code implement the functionality you want?
This need Workbook sheets "Input" & "Output"

VBA Code:
Sub TS_maxconrep() ' for mrexcel.com forum https://www.mrexcel.com/board/threads/how-to-find-the-maximum-consecutive-repeated-value-on-the-bases-of-two-column-in-vba-excel.1191857/
Dim TmpArr As Variant, Key As Variant, Arr() As Variant, StrArr As Variant
Dim TmpRng As Range, ReturnRNG As Range
Dim rw As Long, TmpStr As String, i As Long
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")

With Sheets("Input")
'***** Creating source Range *********************************************************
    rw = .Range("A65536").End(xlUp).Row
    Set TmpRng = .Range("A2" & ":" & "B" & rw)
'***** Creating array ****************************************************************
    TmpArr = TmpRng.Value

'***** ARRAY VALUES TO DICTIONARY ****************************************************
        For i = LBound(TmpArr) To UBound(TmpArr)
            TmpStr = CStr(TmpArr(i, 1) & ":" & TmpArr(i, 2))
            If dict.Exists(TmpStr) Then
                dict(TmpStr) = dict(TmpStr) + 1
            Else
                If Len(TmpStr) > 0 Then dict.Add (TmpStr), 1
           End If
        Next i

End With

'***** FIT ARRAY SIZE TO DICTIONARY **************************************************
ReDim Arr(1 To dict.Count, 1 To 3)
i = 1

'***** DICTIONARY VALUES TO ARRAY ****************************************************
For Each Key In dict.Keys '
    StrArr = split(Key, ":")
        Arr(i, 1) = StrArr(0)
        Arr(i, 2) = StrArr(1)
        Arr(i, 3) = dict(Key)
        i = i + 1
 Next Key
 
 Sheets("Output").Range("A2").Resize(UBound(Arr, 1), 3).Value = Arr
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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