Find lengh of string between . and .

Shadow123

Board Regular
Joined
Aug 20, 2012
Messages
124
I would like to look at each cell in column C, and determine if there are 5 char between the first . and the second .

I can do the for each cell in range part, but i cant make the . to . length work. For arguements sake lets assume that i want to display the value in column C in a message box if the there are 5 char between the first dot and the second dot in the string.

Eg
ABC.APR13.34235.O (This one shows up in message box)
ABCD.D(C).23123.T (This one doesnt show up as its 4 char between dots)
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Assumes your data start in cell A1 - adjust to suit.
Code:
Sub BetweenTheDots()
Dim lR As Long, c As Range
Dim firstDot As Long, secondDot As Long
lR = Range("A" & Rows.Count).End(xlUp).Row
'assume data start in A1 - adjust to suit
For Each c In Range("A1", "A" & lR)
    firstDot = InStr(c.Value, ".")
    If firstDot > 0 Then
        secondDot = InStr(firstDot + 1, c.Value, ".")
        If secondDot > 0 Then
            If secondDot - firstDot = 6 Then
                MsgBox Mid(c.Value, firstDot + 1, 5) & " found in cell " & c.Address
            Else
                MsgBox "Number of characters between first two dots in cell " & c.Address & " is not = 5"
            End If
        Else
            MsgBox "Only one dot found"
        End If
    Else
        MsgBox "No dots found"
    End If
Next c
End Sub
 
Upvote 0
Another way
If we could assume that the last eight characters will always be .#####.A then:
Code:
Sub test()
Dim c As Range
With ActiveSheet
For Each c In .Range("C2", .Cells(Rows.Count, 3).End(xlUp))
    If Len(c) - (InStr(c, ".") + 8) = 5 Then
        MsgBox c.Address
    End If
Next
End With
End Sub
 
Upvote 0
Maybe:
Code:
Sub test()
Dim c As Range
With ActiveSheet
For Each c In .Range("C2", .Cells(Rows.Count, 3).End(xlUp))
    If Len(split(c,".")(1)) = 5 Then
        MsgBox c.Address
    End If
Next
End With
End Sub
 
Upvote 0
Instr can be teamed with InstrRev in VBA.. InstrRev is the Right to Instr's Left ..

From an immediates window..
?instr(1,"1.11111.1",".")
2
?instrrev("1.11111.1",".")
8

Hope that helps..
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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