Solved

Count the number of cells with text below a number and output the result in separate columns

Posted on 2013-05-16
7
486 Views
Last Modified: 2013-05-18
Hi<
I have a fairly straightforward problem - unfortunately, I am in a bit of a time crunch. The issue is that have one column of cells which contain either cells with only numbers or cells that are mostly text but may include a number at the beginning of the variable - the last character would always be text though.. I need to count the number of cells  with text in them below each cell that contains a number and then write the count beside that cell with the number in it. For example here is my column:

1111
erere
2227
ggg
1313
cv
df
gh
1557
df
1gh
ty
yu
5nm
io
2223
uy
bn
qk
dk
wk

So what I would like to end up with is a macro or vba routine that will output the following in a different columns nearby:

1111      1
2227      1
1313      3
1557      6
2223      5


This is just a sample - the actual column contains approximately 3400 cells with probably approx 2000 containing numbers with varying numbers of cells below each of them containing text (and perhaps starting with a number).
0
Comment
Question by:scurvylion
7 Comments
 

Author Comment

by:scurvylion
ID: 39173831
Forgot to indicate that the numbers are each 7 digits long. The cells with text have varying numbers of characters.
0
 
LVL 16

Expert Comment

by:santoshmotwani
ID: 39173838
y dont you use pivot table ... its easy to use and will fix your problem
example.xlsx
0
 

Author Comment

by:scurvylion
ID: 39173859
No i need the count of the cells with text under each each number. Your answer gives the count of cells under number 1111 as 20 when it is 1.

After determining that the count of cells under 111 is 1 then I need the count of cells with text under 2227 and then the count of cells with text under 1313 and so on.

see my example again.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:scurvylion
ID: 39173863
The numbers are random 7 digit numbers while the cells with text are varying lengths and there can be any number of them under each number.
0
 
LVL 2

Accepted Solution

by:
mensmaximus earned 500 total points
ID: 39174237
A quick way to accomplish this task:

Sub CountCellsWithLetters()

    Dim myrow, mycolumn, lastrow, myreportrow, myreportcol, mynumber As Long
    Dim myarea, mycell, myprintnumber, myprintcount As Range
    Dim mycount As Integer
    
    myrow = ActiveCell.Row
    mycolumn = ActiveCell.Column
    myreportrow = myrow
    myreportcol = mycolumn + 1
    
    lastrow = ActiveSheet.Cells(65536, myrow).End(xlUp).Row + 1
    Set myarea = ActiveSheet.Range(Cells(myrow, mycolumn), Cells(lastrow, mycolumn))
    
    For Each mycell In myarea
        If IsNumeric(mycell.Value) Then
            If mycount <> 0 Then
                myprintnumber.Value = mynumber
                myprintcount.Value = mycount
                myreportrow = myreportrow + 1
            End If
            mynumber = mycell.Value
            mycount = 0
        ElseIf mycell.Value Like "*[a-zA-Z]*" Then
            If mycount = 0 Then
                Set myprintnumber = ActiveSheet.Cells(myreportrow, myreportcol)
                Set myprintcount = ActiveSheet.Cells(myreportrow, myreportcol + 1)
            End If
            mycount = mycount + 1
        End If
    Next
   
End Sub

Open in new window


The macro will start from the active cell down to the last row containing any value. If your range contains empty cells set lastrow manually.

Select the cell from where you want start to count and fire the makro.

myreportcol and myreportrow allow you to manipulate the starting column and row of the output.
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 39174411
Assuming data in column A, cells A2:A22 for sample above, a fairly simple formula driven solution:

B2 =ISNUMBER(A2)  Result will be TRUE or FALSE, Copy down as required.
C2 =IF(ISNUMBER(A2),A2,C1)  Result will be copy of column A for new numbers or repeat of number above for text lines, copy down as required.
D2 =COUNTIF($C$2:$C$22,C2)-1  Result will count of cells matching C2, less 1 if you need to allow for first "title" row. Change range in COUNTIF(range,criteria) to allow for all cells.

Select range and activate AutoFilter, in column B select dropdwon and choose TRUE. Hide columns B & C if so required.

Thanks
Rob H
0
 

Author Closing Comment

by:scurvylion
ID: 39177373
Excellent work - and very timely. Thanks again for your assisatnce.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

747 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