Create a macro: formula to show count how many consecutive alphabet in a spreadsheet

Posted on 2012-09-20
Last Modified: 2012-09-20
See attached sheet.

I  need to show the following :

Column P to have a show how many times "A" occured consecutively for 3 times

Column Q to show how many times "A" occured consecutively for 6 times

Column R to show how many times "A" occured consecutively for more than 6 times
Question by:ceneiqe
    LVL 24

    Expert Comment

    OK in the workbook attached is the following formula:

    This allows you to select what you are looking for and how many times to triger the count...
    Function ConsecutiveCount(Letter As String, Occurence As Integer, TheRange As Range) As Integer
    Dim i As Long
    For Each c In TheRange
        If c.Value = Letter Then
            i = i + 1
            If i >= Occurence Then ConsecutiveCount = ConsecutiveCount + 1
            i = 0
        End If
    End Function

    Open in new window

    Please note that the second "A" in about the third line had a space in it... so was in error...
    maybe a find replace " " to correct this on all or changing the  If c.Value = Letter Then to  If c.Value like "*" & Letter & "*" Then will fix that.

    Author Comment

    "Please note that the second "A" in about the third line had a space in it... so was in error..."

    I didn't see any error. Consecutives-count--1-.xls

    Which exact cell are you refering to ?

    Is it possible to run auto by pressing alt f8 and run ?
    LVL 24

    Accepted Solution

    There is no need to 'run' the Function will behave like other in-build Excel functions.
    You just need to change the number of occurences from 3.

    If you chnage it to 1 then you will effectively get a "count", this will highlight the rows with spaces in them.

    Attached file will ignore spaces and work with your existing data.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
    Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
    The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now