0

I tried to create a VBA code that convert a string (in an array) to a new string (which also in an array). However, i'm getting a problem with those original strings that almost identical. E.g. "Power Partner", "Power Partner OEM".

Is there any way I can convert them accurately? Thanks!

Private Function cvrt(orig As Variant) As Variant

    Dim orgVal As Variant, newVal As Variant, i As Integer

    cvrt = orig

    orgVal = Array("Central", "Clarke", "Power Partner", "Power Partner OEM")
    newVal = Array("Central Data", "Clarke Data", "Onsite Power Partner", "Onsite Energy OEM")

    For i = 0 To 3
        cvrt = Replace(cvrt, orgVal(i), newVal(i), vbTextCompare)
    Next i

End Function
5
  • What problem are you getting? What is the value of orig that you are passing to the function? Commented May 29, 2017 at 6:52
  • I'm getting a problem with the last two strings in my code as they are almost identical. When I execute the code, the result for "Power Partner OEM" is incorrect, it became "Onsite Onsite Energy OEM" Commented May 29, 2017 at 6:57
  • What is the value of orig that you are passing to the function? Commented May 29, 2017 at 6:57
  • the value of orig came from a range, which is technically listed in the orgVal Commented May 29, 2017 at 7:01
  • Aha! I didn't see you already accepted an answer. I was testing the code. Anyways, I still posted it for future visitors :) Commented May 29, 2017 at 7:17

2 Answers 2

1

If you dont want to enter the complex world of regular expressions,

  • do it in 2 phases using an intermediate replacement
  • Follow the rule of putting the "largest strings first" in the original array. Hence Power Partner OEM should come before Power Partner.

.

Private Function cvrt(orig As Variant) As Variant
    Dim i As Long, orgVal, newVal, interm
    cvrt = orig
    orgVal = Array("Power Partner OEM", "Power Partner", "Central", "Clarke")
    interm = Array("xxx0", "xxx1", "xxx2", "xxx3")
    newVal = Array("Onsite Energy OEM", "Onsite Power Partner", "Central Data", "Clarke Data")
   For i = 0 To 3
       cvrt = Replace(cvrt, orgVal(i), interm(i), vbTextCompare)
   Next i
   For i = 0 To 3
       cvrt = Replace(cvrt, interm(i), newVal(i), vbTextCompare)
   Next i
End Function

Notice the intermediate array is not necessary, you can use "xxx" & i everywhere instead of interm(i)...

Sign up to request clarification or add additional context in comments.

Comments

1

I'm getting a problem with the last two strings in my code as they are almost identical. When I execute the code, the result for "Power Partner OEM" is incorrect, it became "Onsite Onsite Energy OEM" – kulapo

the value of orig came from a range, which is technically listed in the orgVal – kulapo

The most simplest way to do it is not to use Variants and use Range and then use the inbuilt Range.replace with xlWhole to avoid false positive matches.

Private Function cvrt(orig As Range) As Range '<~~~~~
    Dim orgVal As Variant, newVal As Variant, i As Integer
    Dim rng As Range
    
    Set rng = orig
    
    orgVal = Array("Central", "Clarke", "Power Partner", "Power Partner OEM")
    newVal = Array("Central Data", "Clarke Data", "Onsite Power Partner", "Onsite Energy OEM")

    For i = 0 To 3
        rng.Replace What:=orgVal(i), Replacement:=newVal(i), LookAt:=xlWhole
    Next i
    
    Set orig = rng
End Function

Screenshot

enter image description here

Sub Sample()
    Dim r As Range
    
    Set r = cvrt(Range("A1:A4"))
End Sub


Private Function cvrt(orig As Range) As Range
    Dim orgVal As Variant, newVal As Variant, i As Integer
    Dim rng As Range
    
    Set rng = orig
    
    orgVal = Array("Central", "Clarke", "Power Partner", "Power Partner OEM")
    newVal = Array("Central Data", "Clarke Data", "Onsite Power Partner", "Onsite Energy OEM")

    For i = 0 To 3
        rng.Replace What:=orgVal(i), Replacement:=newVal(i), LookAt:=xlWhole
    Next i
    
    Set orig = rng
End Function

1 Comment

I will also try this way because of its simplicity. Thanks @SiddharthRout!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.