how to count within a cell

ibcarolek

New Member
Joined
Mar 28, 2008
Messages
8
I need to count the number of commas within a cell so I can insert the proper number of rows below before I parse the value into separate rows. For instance,

box, animal, elephant, wood, storage

I need to know that there are 4 commas.
So I can then insert 4 rows below the line and run the following macro:

For i = 1 To Selection.Rows.Count
Dim splitValues As Variant
splitValues = Split(Selection.Rows(i).Value, ",")
Selection.Rows(i).Resize(UBound(splitValues) - LBound(splitValues) + 1).Value = Application.Transpose(splitValues)
Next i

to end up with

box
animal
elephant
wood
storage

thanks for any/all help!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
you could just count the number of elements in your 'slpitValues' array.

newRowCount = ubound(splitValues)+1, if your arrays are set to begin at element 0.
 
Upvote 0
You shouldn't need to select anything. Can you post the complete code.
 
Upvote 0
If you are looking at a particular cell then perhaps

Code:
NCommas = Len(Selection.Value) - Len(Replace(Selection.Value, ",", ""))
 
Upvote 0
VoG, you rock!
Your formula inserts the right number of rows.
So what I got is:

Dim i As Long
Dim Ncommas As Long
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False

Ncommas = Len(Selection.Value) - Len(Replace(Selection.Value, ",", ""))
Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(Ncommas, 0)).Select
Selection.EntireRow.Insert
ActiveCell.Offset(-1, 0).Select
For i = 1 To Selection.Rows.Count
Dim splitValues As Variant
splitValues = Split(Selection.Rows(i).Value, ",")
Selection.Rows(i).Resize(UBound(splitValues) - LBound(splitValues) + 1).Value = Application.Transpose(splitValues)
Next i
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
ActiveCell.Offset(0, -1).Select
End With

Thanks!
 
Upvote 0
sorry...that's
Code:
Dim i As Long
Dim Ncommas As Long 
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
 
Ncommas = Len(Selection.Value) - Len(Replace(Selection.Value, ",", ""))
Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(Ncommas, 0)).Select
Selection.EntireRow.Insert
ActiveCell.Offset(-1, 0).Select
For i = 1 To Selection.Rows.Count
Dim splitValues As Variant
splitValues = Split(Selection.Rows(i).Value, ",")
Selection.Rows(i).Resize(UBound(splitValues) - LBound(splitValues) + 1).Value = Application.Transpose(splitValues)
Next i
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
ActiveCell.Offset(0, -1).Select
End With
 
Upvote 0

Forum statistics

Threads
1,214,414
Messages
6,119,375
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top