Pedro Lastra
Pedro Lastra

Reputation: 63

VBa. Replace data from an array

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.

Upvotes: 0

Views: 9286

Answers (3)

Gary's Student
Gary's Student

Reputation: 96791

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

Upvotes: 2

Siva
Siva

Reputation: 1149

Sub replaceDATA()
 For Each item In UrArray
     item = Replace(item, Left(item, 6), "Your expected string")
 Next
End Sub

Upvotes: 1

user5721973
user5721973

Reputation:

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

Upvotes: 1

Related Questions