Solved

# Find and count code

Posted on 2011-05-03
217 Views
Hi,

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"

Thanks
Seamus
test.xls
0
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
``````
0

LVL 12

Expert Comment

And here's the file
test-sdw.xls
0

LVL 9

Expert Comment

Another option
``````Sub CountText()

Dim Found As Boolean
Dim I As Integer

Sheets("Unit Trust").Select
Range("A1").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
Else
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 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
Loop

Sheets("Update").Range("O29").Value = Cnt_Dvd
Sheets("Update").Range("O30").Value = Cnt_Cpr

End Sub
``````
0

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,
Seamus
0

## Featured Post

### Suggested Solutions

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.