Need VBA code with "building" as string for "=len(a1)-len(substitute(a1,",",""))+1"

Kichan

Board Regular
Joined
Feb 21, 2022
Messages
63
Office Version
  1. 2010
Platform
  1. Windows
Hi
I have a formula =len(a1)-len(substitute(a1,",",""))+1 please give a vba code for this formula with "building" as string

New Microsoft Office Excel Worksheet (4).xlsx
AB
1134-1,134-5,136-83
2134-1,134-52
3134-11
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1


Thank you
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
please give a vba code for this formula with "building" as string
I'm not exactly sure what you meant by that, but if you are just trying to replicate the formula results using vba, then would this be any use?

VBA Code:
Sub Count_Items()
  Dim a As Variant
  Dim i As Long
  
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      a(i, 1) = UBound(Split(a(i, 1), ",")) + 1
    Next i
    .Offset(, 1).Value = a
  End With
End Sub
 
Upvote 0
I'm not exactly sure what you meant by that, but if you are just trying to replicate the formula results using vba, then would this be any use?

VBA Code:
Sub Count_Items()
  Dim a As Variant
  Dim i As Long
 
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      a(i, 1) = UBound(Split(a(i, 1), ",")) + 1
    Next i
    .Offset(, 1).Value = a
  End With
End Sub
sorry brother .. ok now i will tell you what was my real requirment was

I want to get total number of items before comma "," in a single cell . that is in cell A1 I wrote134-1,134-5,136-8 in this case the items before "," is "3" so the total number of items in that cell is "3" .. like all other cells items before comma is counted

one more thing can you make this code ("building" as string) or ("kich" as string) with out declaring (Dim) items .. so i can use it in any cell as =kich(A1)
thanks
 
Upvote 0
so i can use it in any cell as =kich(A1)
How would that be an advantage over using the standard worksheet formula from post #1 since worksheet functions are usually more efficient than a user-defined vba function equivalent?
 
Upvote 0
Try this:

PHP:
Public Function kich(ByVal s As String) As Long
If Len(s) > 0 Then kich = UBound(Split(s, ",")) + 1
End Function
 
Upvote 0
Solution
How would that be an advantage over using the standard worksheet formula from post #1 since worksheet functions are usually more efficient than a user-defined vba function equivalent?
Post #5 provides a good example of my point. Tested on the same range of data several times, the udf took almost 10 times as long to calculate my test range than the post #1 formula.
Whilst the difference may not be a problem unless the ranges are very large, I just don't see any advantage of using such a udf over the worksheet formula.
 
Upvote 0
Post #5 provides a good example of my point. Tested on the same range of data several times, the udf took almost 10 times as long to calculate my test range than the post #1 formula.
Whilst the difference may not be a problem unless the ranges are very large, I just don't see any advantage of using such a udf over the worksheet formula.
Thank you bro for your reply ... I have started a VBA macros online course , and just a beginner ... From this platform getting lot of solutions from different angles on the same question .. This is really helping me to improve ..

Thank you .. have a good day brother
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,772
Members
449,049
Latest member
greyangel23

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