Help With User Defined Function

anwaee2

Board Regular
Joined
Sep 13, 2012
Messages
151
Office Version
  1. 2011
Platform
  1. MacOS
I haver a user defined function to take the second to last word in a string and add 1 to it. When I run the macro with the function in it, I get "Sub Or Function Not Defined". When I go to the debugger, the "Rept" function is highlighted.
User Defined Function:

Public Function SECONDWORD(sw As String) As String
ln = Trim(Left(Right(" " & Substitute(Trim(sw), " ", Rept(" ", 60)), 120), 60))+1
End Function

Thanks in advance for any help I may get.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Neither Rept, nor Substitute exist in VBA.
Also that function won't return anything.

Can you please post a few examples of your data & what you want it to look like?
 
Upvote 0
Hello anwaee2

If what you need to is find the last word in a string, that is the word to the right of the last space character in a string then why can't you simply use:

RIGHT( text, [number_of_characters] )
Where number_of_Characters can be the position of the last space +1? The + 1 is to get to the character that starts the last word.

Would that not work in your strings?
 
Upvote 0
Rich (BB code):
Public Function SECONDWORD(sw As String) As String
Dim wf As Variant
Set wf = WorksheetFunction
SECONDWORD = Trim(Left(Right(" " & wf.Substitute(Trim(sw), " ", wf.Rept(" ", 60)), 120), 60))  ' no +1
End Function

I have not tested that extensively, but it does return "second" when the Excel formula is =secondword("first second third").

Note: The VBA alternatives to wf.Substitute and wf.Rept are Replace and String. I did not bother to see how they might work (or not) with your design.
 
Upvote 0
user defined function to take the second to last word in a string and add 1 to it .

Code:
Public Function SECONDWORD(sw As String) As String
Dim x As Variant
    x = Split(sw, " "): x(0) = 1:  x = Join(x)
    SECONDWORD = x
End Function
 
Upvote 0
Maybe
Code:
Function anwaee2(sw As String) As String
   Dim sp As Variant
   sp = Split(sw)
   sp(UBound(sp) - 1) = sp(UBound(sp) - 1) & 1
   anwaee2 = Join(sp, " ")
End Function
or
Code:
Function anwaee2(sw As String) As String
   Dim sp As Variant
   sp = Split(sw)
   anwaee2 = sp(UBound(sp) - 1) & 1
End Function
 
Upvote 0
joeu2004, thank you so much. I have googled and searched through 3 books i have with no results. But your code worked perfectly. I have not tried the others yet but wish to thank all for the replies. Thanks again to all.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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