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
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This should work in either Access or Excel vba - if it's in a standard module. I've no experience with Project vba so you'd have to try.
VBA Code:
Function GetToDotSix(strIn As String) As String
Dim i As Integer, n As Integer
'call as someThing = GetToDotSix("1.2.3.3n.5.9n.10.8n.9n.10n")

For i = 1 To Len(strIn)
  If Mid(strIn, i, 1) = "." Then n = n + 1
     If n = 6 Then
       MsgBox Left(strIn, i - 1)
       Exit Function
   End If
Next

End Function
Instead of hard coding string, perhaps you would reference the cell object from Project.
EDIT - You'd have to do something other than a msgbox of course. That code doesn't worry about less than 6 dots or if there is no value.
 
Last edited:
Upvote 0
This should work in either Access or Excel vba - if it's in a standard module. I've no experience with Project vba so you'd have to try.
VBA Code:
Function GetToDotSix(strIn As String) As String
Dim i As Integer, n As Integer
'call as someThing = GetToDotSix("1.2.3.3n.5.9n.10.8n.9n.10n")

For i = 1 To Len(strIn)
  If Mid(strIn, i, 1) = "." Then n = n + 1
     If n = 6 Then
       MsgBox Left(strIn, i - 1)
       Exit Function
   End If
Next

End Function
Instead of hard coding string, perhaps you would reference the cell object from Project.
Thank you so much! I believe this is a good starting point for some experimentation on my part because yes, Project VBA is a bit different than Excel, so I'll have to modify it somehow to run in Project. Thanks again, I know it's a pain when given little to work with but I appreciate your time!
 
Upvote 0
Slight mod; will handle cases where input has less than six .
It already would handle if input was an empty string - I just overlooked that. Please do post your code when you figure it out. I'd like to learn how you did it.
VBA Code:
Function GetToDotSix(strIn As String) As String
Dim i As Integer, n As Integer
'call as GetToDotSix("1.2.3.3n.5.9n.10.8n.9n.10n")

For i = 1 To Len(strIn)
    If Mid(strIn, i, 1) = "." Then n = n + 1
    If n = 6 Then
       MsgBox Left(strIn, i - 1)
       GetToDotSix = Left(strIn, i - 1)
       Exit For
    End If
Next

If n > 0 And n < 6 Then MsgBox "Not enough dot characters to process"

End Function

EDIT - I've bookmarked Access portion of Office object model. Here's the Project section in case you can use it.
 
Upvote 0
Slight mod; will handle cases where input has less than six .
It already would handle if input was an empty string - I just overlooked that. Please do post your code when you figure it out. I'd like to learn how you did it.
VBA Code:
Function GetToDotSix(strIn As String) As String
Dim i As Integer, n As Integer
'call as GetToDotSix("1.2.3.3n.5.9n.10.8n.9n.10n")

For i = 1 To Len(strIn)
    If Mid(strIn, i, 1) = "." Then n = n + 1
    If n = 6 Then
       MsgBox Left(strIn, i - 1)
       GetToDotSix = Left(strIn, i - 1)
       Exit For
    End If
Next

If n > 0 And n < 6 Then MsgBox "Not enough dot characters to process"

End Function

EDIT - I've bookmarked Access portion of Office object model. Here's the Project section in case you can use it.
I got it to work on only the first row using this modified version of your code (MS Project does not seem to be able to use UDFs)

I'm obviously messing up the For loop. Any ideas on how to fix it?

Thanks!

VBA Code:
Sub GetToDotSix()

Dim i As Integer, n As Integer

Dim tsk As Task

Dim taskList As Tasks


Set taskList = ActiveProject.Tasks

For Each tsk In ActiveProject.Tasks

If Not tsk Is Nothing Then

For i = 1 To Len(tsk.Text1)

If Mid(tsk.Text1, i, 1) = "." Then n = n + 1

If n = 6 Then

tsk.Text2 = Left(tsk.Text1, i - 1)

Exit For

End If

Next

If n > 0 And n < 6 Then MsgBox "Not enough dot characters to process"

End If

Next

End Sub
Result: Result: Result:
 
Upvote 0
All 3 images look the same to me? Looks like you pasted that code from something that injects line wraps. Between that and the lack of indentation I don't even want to read it (it's just too hard). Attempting to fix that:
VBA Code:
Sub GetToDotSix()
Dim i As Integer, n As Integer
Dim tsk As Task
Dim taskList As Tasks

Set taskList = ActiveProject.Tasks
For Each tsk In ActiveProject.Tasks
   If Not tsk Is Nothing Then
      For i = 1 To Len(tsk.Text1)
         If Mid(tsk.Text1, i, 1) = "." Then n = n + 1
         If n = 6 Then
            tsk.Text2 = Left(tsk.Text1, i - 1)
            Exit For
         End If
      Next
      If n > 0 And n < 6 Then MsgBox "Not enough dot characters to process"
   End If
Next

End Sub
I can't imagine that udf's cannot be used in project vba but can't test that. Perhaps your udf does not have the required scope.
Suggest you step thru the code (F8) and watch your variables and such. You don't SET your tsk variable to anything, so I imagine that's a problem, assuming it is an object. After that, report back and if you discovered no issues I'll see if I can see a problem with the loop.
Also, at the end it's advisable to set object variables to Nothing - at least in other Office vba code.
Have to wonder why the test for Is Nothing? Can you have a task as a member of a collection of Tasks and loop thru and find that one is 'nothing'?
 
Upvote 0
Sorry about the formatting!

The code below fixed my problem.

I really appreciate your help and effort in this!



VBA Code:
Sub Sixth_dot() 
Dim t As Task 
Dim T1_String() As String 
Dim t2_string As String 

For Each t In ActiveProject.Tasks    
          T1_String() = Split(t.Text1, ".")    
          If UBound(T1_String) < 5 Then        
               t.Text2 = "Not enough dot characters to process"    
          Else        
               For i = LBound(T1_String) To 5            
                    If i = 0 Then                
                              t.Text2 = T1_String(I)            
                    Else                
                              t.Text2 = t.Text2 & "." & T1_String(I)            
                    End If        
               Next I    
          End If 
Next t
 
End Sub
 
Upvote 0
Solution
I gather that you were able to get code from somewhere else, which looks interesting because I considered an array but didn't know how I could use it with Project. I take it that you are in fact able to use udf's in Project. Thanks for posting a solution. You might as well mark your post as the solution.
 
Upvote 0
I gather that you were able to get code from somewhere else, which looks interesting because I considered an array but didn't know how I could use it with Project. I take it that you are in fact able to use udf's in Project. Thanks for posting a solution. You might as well mark your post as the solution.
Yes, the code was from another source. Again, really appreciate your time and effort!
 
Upvote 0
You're welcome! Good luck with your Project project!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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