Find and count code

Posted on 2011-05-03
Last Modified: 2012-05-11

I have attached a ss where i would like a sub to look for "Dividend" and move one down and count until the blanks, in this example that would be 4. Then paste that figure into the update tab in cell O29.

Then repeat for "Corporate Action"

Question by:Seamus2626
    LVL 12

    Accepted Solution

    This is not beautiful, but it's understandable and it works.
    Sub FindMyText()
    endRow = Sheets("Unit Trust").Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To endRow
      If UCase(Left(Sheets("Unit Trust").Range("A" & i).Value, 8)) = "DIVIDEND" Then
        firstDividend = i
      End If
      If lastDividend = "" Then
        If Sheets("Unit Trust").Range("A" & i).Value = "" And firstDividend <> "" Then
          lastDividend = i - 1
        End If
      End If
      If UCase(Left(Sheets("Unit Trust").Range("A" & i).Value, 16)) = "CORPORATE ACTION" Then
        rowCorporate = i
        Exit For
      End If
    Next i
    Sheets("Update").Range("O29").Value = lastDividend - firstDividend
    Sheets("Update").Range("O30").Value = endRow - rowCorporate
    End Sub

    Open in new window

    LVL 12

    Expert Comment

    And here's the file
    LVL 9

    Expert Comment

    Another option
    Sub CountText()
    Dim Found As Boolean
    Dim I As Integer
    Sheets("Unit Trust").Select
     Do Until Selection.Row > Sheets("Unit Trust").Cells(Sheets("Unit Trust").Rows.Count, "A").End(xlUp).Row
        If Selection.Value <> "" Then
            If UCase(Left(Selection.Value, 8)) = "DIVIDEND" Then
                dvd = True
                cpr = False
                mRow = Selection.Row
            ElseIf UCase(Left(Selection.Value, 16)) = "CORPORATE ACTION" Then
                dvd = False
                cpr = True
                mRow = Selection.Row
                Found = False
                For I = mRow To Selection.Row - 1
                    If ActiveSheet.Cells(I, 1).Value = Selection.Value Then
                        Found = True
                        Exit For
                    End If
                Next I
                If Not Found Then
                    If dvd Then
                        Cnt_Dvd = Cnt_Dvd + 1
                    ElseIf cpr Then
                        Cnt_Cpr = Cnt_Cpr + 1
                    End If
                End If
            End If
        End If
        Selection.Offset(1, 0).Select
    Sheets("Update").Range("O29").Value = Cnt_Dvd
    Sheets("Update").Range("O30").Value = Cnt_Cpr
    End Sub

    Open in new window


    Author Closing Comment

    Thanks, thats perfect

    Chwong, your sub put 3+9 into the sheet where it should be 4+22

    Thank you as well

    Kind Regards,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

    760 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

    11 Experts available now in Live!

    Get 1:1 Help Now