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!
 

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,196
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.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
You shouldn't need to select anything. Can you post the complete code.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
If you are looking at a particular cell then perhaps

Code:
NCommas = Len(Selection.Value) - Len(Replace(Selection.Value, ",", ""))
 

ibcarolek

New Member
Joined
Mar 28, 2008
Messages
8
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!
 

ibcarolek

New Member
Joined
Mar 28, 2008
Messages
8
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
 

Forum statistics

Threads
1,084,735
Messages
5,379,509
Members
401,608
Latest member
nanana2020

Some videos you may like

This Week's Hot Topics

  • VBA code giving errors and stopping Excel
    Hello Experts, I have this code being used to loop through files in a file path, and copy specific data to another sheet. It is giving me several...
  • Disable MsgBox message
    Morning, I have a userform where if i leave a ComboBox empty i see a MsgBox warning me that i must enter an invoice number. It is this MsgBox i...
  • Macro Recorder into VBA, Copy Paste Data Filled Cells
    Hi Everyone, I have a macro recorder file that takes a selection of data, copies, then pastes into a new sheet on ("A2:B2") The issue is my...
  • Number format changes while pasting into a cell
    Hi, I am trying to paste a number 180204524303 from an email to an excel cell, however, whenever i try to do so , the the paste value appears as...
  • Collating data
    Hello all. Could someone please help. I am trying to pull all column data from multiple sheets (24 I total so far) into 1 master sheet without...
  • Sum Multiple Columns Based on Multiple Criteria
    I am trying to consolidate data by summing columns G through M based on material, plant, vendor, and fiscal year being identical. The period does...
Top