Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA: position of substring in a comma-delimited string

Posted on 2009-07-06
3
Medium Priority
?
970 Views
Last Modified: 2013-11-26
Hi X-perts,

I have a comma delimited string like

mystring = "(number1,number2,number3, number4)

or (2343, 7765, 9928, 3354)

I need a function that returns a relative position of a given number inside that string, i.e.

myFunction(mystring, mynumber)

myFunction(mystring, 2343) = 1

etc.

what is the simplest way of doing this? In php it would take just two lines of code with explode/implode, but I am not sure what is the closest option in VBA.

Please, advise..... it needs to be a very short solution

Thanks
0
Comment
Question by:andy7789
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 24783703
Easiest way, with the help of split-function
Sub test()
    MsgBox GetPosition("(2343, 7765, 9928, 3354)", "7765")
End Sub
 
Public Function GetPosition(inString As String, searchString As String) As Integer
    Dim strSplit() As String
    GetPosition = -1
    
    ' removing ( and )
    inString = Replace(inString, "(", "")
    inString = Replace(inString, ")", "")
    strSplit = split(inString, ",")
    For i = 0 To UBound(strSplit)
        If Trim(strSplit(i)) = searchString Then
            GetPosition = i + 1
        End If
    Next i
    
End Function

Open in new window

0
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 1000 total points
ID: 24783753
If you are using VBA within Excel, you can use the below function, which also shows usage.

Wayne
Sub test()
 
     Dim mystring As String
     mystring = "(2343, 7765, 9928, 3354)"
 
     MsgBox IndexOf(mystring, "9928") 'will return 3
 
End Sub
 
Public Function IndexOf(strArray As String, item As String) As Integer
 
     Dim arr() As String
     arr = Split(Replace(Replace(Replace(strArray, "(", ""), ")", ""), " ", ""), ",")
 
     Dim v As Variant
     v = Application.Match(item, Application.Transpose(arr), 0)
     If IsError(v) Then
          IndexOf = -1
     Else
          IndexOf = v
     End If
 
End Function

Open in new window

0
 

Author Comment

by:andy7789
ID: 24784092
Thank you
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question