Automatically merge cells within range while entering highest count

%3CLINGO-SUB%20id%3D%22lingo-sub-1733095%22%20slang%3D%22en-US%22%3EAutomatically%20merge%20cells%20within%20range%20while%20entering%20highest%20count%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1733095%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20there%3CBR%20%2F%3E%3CBR%20%2F%3EI%20would%20like%20to%20know%20a%20formula%20that%20encompasses%20the%20following%20criterias%3A%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E-%20Count%20the%20number%20of%20rows%20within%20the%20same%20hour%20range%20value%20(extract%20highest%20count%20value)%3CBR%20%2F%3E-%20Merge%20cells%20belonging%20to%20the%20same%20hour%20range%20value%20and%20paste%20the%20highest%20count%20value%20within%20merged%20cell%3CBR%20%2F%3E-%20Repeat%20the%20process%20until%20last%20non-empty%20cell%20of%20the%20specified%20column%20(G%20column)%20is%20reached%3CBR%20%2F%3E%3CBR%20%2F%3ENote%3A%3CBR%20%2F%3ETotal%20G%20column%20cell%20count%20are%20not%20fixed%20and%20will%20always%20start%20with%20G1%20and%20will%20end%20with%20an%20unpredictable%20the%20last%20non-empty%20cell%20count%20...%20meaning%20the%20formula%20needs%20to%20first%20identify%20the%20last%20cell%20count%20within%20a%20range%20as%20well%20as%20to%20identify%20the%20last%20non-empty%20cell%20within%20the%20G%20column%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture1.png%22%20style%3D%22width%3A%20412px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F223240i18B9C45FBA6DDE86%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Picture1.png%22%20alt%3D%22Picture1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1733095%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1733390%22%20slang%3D%22en-US%22%3ERe%3A%20Automatically%20merge%20cells%20within%20range%20while%20entering%20highest%20count%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1733390%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F815810%22%20target%3D%22_blank%22%3E%40lhvinsky%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EA%20Formula%20cannot%20merge%20cells%20but%20can%20return%20the%20count%20based%20on%20your%20criteria%20only.%3C%2FP%3E%3CP%3EIf%20you%20want%20to%20merge%20the%20cells%20based%20on%20the%20criteria%20as%20well%2C%20you%20will%20need%20VBA%20to%20achieve%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlace%20the%20following%20code%20on%20a%20Standard%20Module%20like%20Module1%20and%20activate%20the%20sheet%20in%20which%20you%20want%20to%20count%20and%20merge%20the%20cells%20and%20run%20the%20macro%20on%20Module1%20then.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20Sheet1%20in%20the%20attached%2C%26nbsp%3B%26nbsp%3B%20I%20have%20placed%20some%20dummy%20date%20time%20stamps%20in%20column%20G%20and%20inserted%20a%20button%20called%26nbsp%3B%22Count%20and%20Merge%22%20%2C%20you%20may%20click%20this%20button%20to%20run%20the%20code%20to%20get%20the%20desired%20output.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20CountCellsForSameHours()%0ADim%20lr%20%20%20%20%20%20As%20Long%0ADim%20i%20%20%20%20%20%20%20As%20Long%0ADim%20j%20%20%20%20%20%20%20As%20Long%0ADim%20rng%20%20%20%20%20As%20Range%0ADim%20found%20%20%20As%20Boolean%0A%0AApplication.ScreenUpdating%20%3D%20False%0AApplication.DisplayAlerts%20%3D%20False%0A%0Alr%20%3D%20Cells(Rows.Count%2C%20%22G%22).End(xlUp).Row%0A%0AFor%20i%20%3D%201%20To%20lr%0A%20%20%20%20If%20IsDate(Cells(i%2C%20%22G%22))%20Then%0A%20%20%20%20%20%20%20%20If%20Not%20found%20Then%20Set%20rng%20%3D%20Cells(i%2C%20%22H%22)%0A%20%20%20%20%20%20%20%20If%20Hour(Cells(i%2C%20%22G%22))%20%3D%20Hour(Cells(i%20%2B%201%2C%20%22G%22))%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20found%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20j%20%2B%201%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20rng%20%3D%20rng.Resize(j%20%2B%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20rng.Merge%0A%20%20%20%20%20%20%20%20%20%20%20%20rng.Value%20%3D%20j%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20rng.HorizontalAlignment%20%3D%20xlCenter%0A%20%20%20%20%20%20%20%20%20%20%20%20rng.VerticalAlignment%20%3D%20xlCenter%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20rng%20%3D%20Nothing%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20found%20%3D%20False%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20If%0ANext%20i%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1742203%22%20slang%3D%22en-US%22%3ERe%3A%20Automatically%20merge%20cells%20within%20range%20while%20entering%20highest%20count%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1742203%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%20for%20the%20quick%20reply%20...%20everything%20works%20great%20accept%20for%20the%20merged%20cells%20for%20the%20earliest%20hour%20located%20at%20the%20very%20bottom%20(see%20screenshot)%20...%20for%20some%20reason%20it%20is%20not%20merging%20and%20adding%20the%20counted%20result.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22lhvinsky_0-1601741971391.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F223919i052F4FE015A4CA46%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22lhvinsky_0-1601741971391.png%22%20alt%3D%22lhvinsky_0-1601741971391.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi there

I would like to know a formula that encompasses the following criterias:


- Count the number of rows within the same hour range value (extract highest count value)
- Merge cells belonging to the same hour range value and paste the highest count value within merged cell
- Repeat the process until last non-empty cell of the specified column (G column) is reached

Note:
Total G column cell count are not fixed and will always start with G1 and will end with an unpredictable the last non-empty cell count ... meaning the formula needs to first identify the last cell count within a range as well as to identify the last non-empty cell within the G column


Picture1.png

5 Replies
Highlighted

@lhvinsky 

A Formula cannot merge cells but can return the count based on your criteria only.

If you want to merge the cells based on the criteria as well, you will need VBA to achieve this.

 

Place the following code on a Standard Module like Module1 and activate the sheet in which you want to count and merge the cells and run the macro on Module1 then.

 

On Sheet1 in the attached,   I have placed some dummy date time stamps in column G and inserted a button called "Count and Merge" , you may click this button to run the code to get the desired output.

 

Sub CountCellsForSameHours()
Dim lr      As Long
Dim i       As Long
Dim j       As Long
Dim rng     As Range
Dim found   As Boolean

Application.ScreenUpdating = False
Application.DisplayAlerts = False

lr = Cells(Rows.Count, "G").End(xlUp).Row

For i = 1 To lr
    If IsDate(Cells(i, "G")) Then
        If Not found Then Set rng = Cells(i, "H")
        If Hour(Cells(i, "G")) = Hour(Cells(i + 1, "G")) Then
            found = True
            j = j + 1
        Else
            Set rng = rng.Resize(j + 1)
            rng.Merge
            rng.Value = j + 1
            rng.HorizontalAlignment = xlCenter
            rng.VerticalAlignment = xlCenter
            Set rng = Nothing
            j = 0
            found = False
        End If
    End If
Next i
Application.ScreenUpdating = True
End Sub

 

Highlighted

@Subodh_Tiwari_sktneer 

Thank you for the quick reply ... everything works great accept for the merged cells for the earliest hour located at the very bottom (see screenshot) ... for some reason it is not merging and adding the counted result.

lhvinsky_0-1601741971391.png

 

Highlighted

@lhvinsky 

Can you please upload the file with the issue?

Highlighted
Highlighted

@lhvinsky 

Please find the attached and see if this is working as desired now.