Trouble with Excel syntax

dan_m101

New Member
Joined
Nov 26, 2011
Messages
47
I've tried different things but I'm having trouble finding the correct syntax to accomplish the following

In column A I have text like

brut-230x340x450x50:890_capital
sale-120x100:450_trim

there's always a "-" at the beginning of the line followed by a "_" towards the end

these markers do not appear elsewhere on the line, ever

I would like to count the number of x and : between the 2 markers, for example 4 is the answer for the first line and 2 for the second

I can't seem to get the correct vba coding unfortunately :(

Thank you for any help
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try:
VBA Code:
Sub a1181660a()
Dim i As Long
Dim tx As String
Dim va
va = Range("A2", Cells(Rows.Count, "A").End(xlUp))

For i = 1 To UBound(va, 1)
    tx = Split(va(i, 1), "-")(1)
    tx = Split(tx, "_")(0)
    tx = Replace(tx, ":", "x")
    va(i, 1) = UBound(Split(tx, "x"))
Next

Range("B2").Resize(UBound(va, 1), 1) = va
End Sub

Book1
AB
1
2brut-230x340x450x50:890_capital4
3sale-120x100:450_trim2
4sale-120x100:450_trim 2x2:2
Sheet1
 
Upvote 0
thx Akuini, I'm also considering writing a function to handle text in any cell, would the coding look significantly different? thanks again
 
Upvote 0
I'm also considering writing a function to handle text in any cell,
Do you mean a UDF?
VBA Code:
Function dan_m101(c As Range)
Dim tx As String

    tx = Split(c, "-")(1)
    tx = Split(tx, "_")(0)
    tx = Replace(tx, ":", "x")
    dan_m101 = UBound(Split(tx, "x"))

End Function

Book1
AB
1
2brut-230x340x450x50:890_capital4
3sale-120x100:450_trim2
4sale-120x100:450_trim 2x2:2
Sheet1
Cell Formulas
RangeFormula
B2:B4B2=dan_m101(A2)
 
Upvote 0
I'm also considering writing a function to handle text in any cell
In that case, why not use native worksheet functions.
For example, if you have MS365 you could use column B, otherwise column C. Speaking of versions, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

If you still really want a UDF, you could put all of Akuini's clearly set-out steps into a single line
VBA Code:
Function Dan(s As String) As Long
  Dan = UBound(Split(Replace(Split(Split(s, "-")(1), "_")(0), ":", "x"), "x"))
End Function

dan_m101.xlsm
ABCD
1brut-230x340x450x50:890_capital444
2sale-120x100:450_trim222
3sale-120x100:450_trim 2x2:222
4sale-6_dcd000
Sheet1
Cell Formulas
RangeFormula
B1:B4B1=LET(s,SUBSTITUTE(REPLACE(LEFT(A1,FIND("_",A1)),1,FIND("-",A1),""),":","x"),LEN(s)-LEN(SUBSTITUTE(s,"x","")))
C1:C4C1=LEN(SUBSTITUTE(REPLACE(LEFT(A1,FIND("_",A1)),1,FIND("-",A1),""),":","x"))-LEN(SUBSTITUTE(SUBSTITUTE(REPLACE(LEFT(A1,FIND("_",A1)),1,FIND("-",A1),""),":","x"),"x",""))
D1:D4D1=Dan(A1)
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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