Vba for counting commas and adding them

interner

New Member
Joined
Jun 20, 2011
Messages
44
I am trying to write a vba script that counts commas that are visible then adds them to a previously defined variable.

Here's what I have been able to come up with so far, however I know that it won't work because none of the cells have just a comma in them....I am trying to attempt to actually add one to the "counter" variable for every comma that is found within the cell of column "O" or column 15.

Dim rng As Range
Set rng = ActiveSheet.AutoFilter.Range
counter = 0
Last = Cells(Rows.Count, "O").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "O").Value) = "," And Rows(i).EntireRow.Hidden = False_ Then
counter = counter + 1
End If
Next i

MsgBox counter
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I've gotten a little bit farther, my new question should be a little easier to answer.
How do I make it so that x the variable x attempts to cover the entire range of Column "O" rahter then just the particular cell O2?
Sub test()
Last = Cells(Rows.Count, "O").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "O").Value) <> "" And Rows(i).EntireRow.Hidden = False_ Then
x = [Len(O2) - Len(Substitute(O2, ",", ""))]

End If
Next i
MsgBox x
End Sub
 
Upvote 0
before the loop begins, set x to 0

Then instead of

Code:
x = [Len(O2) - Len(Substitute(O2, ",", ""))]

use

Code:
str = cells(i,"O").value
x = x + Len(str) - Len(Substitute(str, ",", ""))

BTW, why the square brackets?

Also, why step through backwards? You may have seen this used elsewhere, but it's only relevant if you're deleting rows or there may be a condition which might stop you testing the entire range.

HTH
 
Last edited:
Upvote 0
lol, I do the backwards thing out of habit, and thanks for your help! I got it

Sub test()
Last = Cells(Rows.Count, "O").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "O").Value) <> "" And Rows(i).EntireRow.Hidden = False_ Then
x = x + Len(Cells(i, "O").Value) - Len(Replace(Cells(i, "O").Value, ",", ""))

End If
Next i
MsgBox x
End Sub


One issue that arose consisted of the word substitute being an incompatible term. So I used "Replace" instead of "substitute" and it worked seamlessly. Thanks again Weav.
 
Upvote 0
One issue that arose consisted of the word substitute being an incompatible term. So I used "Replace" instead of "substitute" and it worked seamlessly. Thanks again Weav.
Ah yes, I forgot about that, what with =SUBSTITUTE() being the worksheet equivalent of VB Replace().
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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