0

I Have an array like this Dim RG4() As String -- Name of the Array

0 (XX-AA-2345)
1 (XX-DA-2454)
2 (XX-CD-9476) 
3 (XX-AA-4711)
4  ...

And i want to replace the beggning of the whole array "XX-DA-" For something standard like for exaple "XX-AA-(numbers)" in all of them.

I´ve done some research and i couldnt find anything that could work here.

3 Answers 3

2

Select your cells and run:

Sub Lastra()
   Dim r As Range
   Set r = Selection
   r.Replace what:="XX-DA", replacement:="XX-AA"
End Sub

EDIT#1:

For a VBA array, you can use a loop:

Sub qwerty()
   Dim RG4(0 To 3), i As Long

   RG4(0) = "0 (XX-AA-2345)"
   RG4(1) = "1 (XX-DA-2454)"
   RG4(2) = "2 (XX-CD-9476)"
   RG4(3) = "3 (XX-AA-4711)"

   For i = 0 To 3
      RG4(i) = Replace(RG4(i), "XX-DA", "XX-AA")
   Next i
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

I don´t have a Range i have an array.This one Dim RG4() As String
@PedroLastra See my EDIT#1:
1
Sub replaceDATA()
 For Each item In UrArray
     item = Replace(item, Left(item, 6), "Your expected string")
 Next
End Sub

Comments

1
For Each Thing in MyArray
    Thing = Replace(thing, "XX", "WhatEverYouWant", 1, 1)
Next

You can also use Mid, Left, Right and string concatenation (&).

Alternative to Replace is a RegExp.

See help https://www.microsoft.com/en-au/download/details.aspx?id=2764

Comments

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.