Display the text before the 6th instance of a period in a string (MS Project or VBA)

sahderach

New Member
Joined
Nov 6, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello!

I'm trying to figure out a macro that can display the text before the 6th instance of a period in a string.

=TEXTBEFORE(".",6) works perfectly in Excel but I need this to work in MS Project and I don't know of a formula that will do this using MS Project's list of commands.

If this is possible using MS Project's limited functions, that would be ideal but I think a macro would be easier.

I'm not very strong in VBA so any help would be greatly appreciated, thanks!

OriginalTEXTBEFORE 6th "."
1.2.3.4.5.6.7.81.2.3.4.5.6
1.2.3n.4.5.5.6.71.2.3n.4.5.5
1n.2n.3n.4n.5.6.7.81n.2n.3n.4n.5.6
1.2.3.3n.5.9n.10.8n.9n.10n1.2.3.3n.5.9n
 
I am late to the party but consider the following:

Formula approach:

Book1
ABCD
1Original StringBefore 6thAfter 6th
21.2.3.4.5.6.7.81.2.3.4.5.67.8
3
41.2.3.4.5.67.8
5
Sheet1
Cell Formulas
RangeFormula
B2B2=LEFT(A2,FIND(CHAR(1),SUBSTITUTE(A2,".",CHAR(1),6))-1)
C2C2=REPLACE(A2,1,FIND(CHAR(1),SUBSTITUTE(A2,".",CHAR(1),6)),"")


VBA approach:

VBA Code:
Sub GetNthCharactersFromACell()
'
'  Display the text before the 6th instance of a period in a string
    Range("B4").Value = Evaluate("=LEFT(A2,FIND(CHAR(1),SUBSTITUTE(A2,""."",CHAR(1),6))-1)")
'
'  Display the text After the 6th instance of a period in a string
    Range("C4").Value = Evaluate("=REPLACE(A2,1,FIND(CHAR(1),SUBSTITUTE(A2,""."",CHAR(1),6)),"""")")
End Sub
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Perhaps better, more adjustable VBA code:

VBA Code:
Sub GetNthCharactersFromACellV2()
'
    Dim NthCharacter            As Long
    Dim CellToCheck             As String
    Dim ReplacementCharacter    As String
'
    NthCharacter = 6                                                                                                        ' <--- Set this to the Nth character that you seek
    CellToCheck = "A2"                                                                                                      ' <--- Set this to the Cell to check
    ReplacementCharacter = "CHAR(1)"                                                                                        ' <--- Set this to the character to mark the string with
'
'  Display the text before the 6th instance of a period in a string
    Range("B4").Value = Evaluate("LEFT(" & CellToCheck & ",FIND(" & ReplacementCharacter & ",SUBSTITUTE(" & CellToCheck & _
            ","".""," & ReplacementCharacter & "," & NthCharacter & "))-1)")                                                ' Evaluate the value of the formula
'
'  Display the text After the 6th instance of a period in a string
    Range("C4").Value = Evaluate("REPLACE(" & CellToCheck & ",1,FIND(" & ReplacementCharacter & ",SUBSTITUTE(" & _
            CellToCheck & ","".""," & ReplacementCharacter & "," & NthCharacter & ")),"""")")                               ' Evaluate the value of the formula
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,611
Members
449,109
Latest member
Sebas8956

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