VB - add to array?

This is probably a very simple question. But I have this array, from which I need to remove the unnecessary parts. But I still want it in an array.

So the array looks like this when it arrives:

ArrValues(0) "Firstname=FIRSTNAME" ArrValues(1) "Lastname=LASTNAME" ArrValues(2) "Username=USERNAME" ArrValues(3) "Displayname=DISPLAYNAME" 

Then I send this array through this piece of code:

 For Each s In arrValues s = Split(s, "=") s = s(1) Next 

This breaks the lines, so I only get FIRSTNAME and so on. But I want to send each cleared string back to the array again. How to do it?

+7
source share
2 answers

As long as you have all the lines with the "=" sign, this code should work ... using the MID function. I am not a performance expert, but I believe that the MID function will have better performance than SPLIT (since split will override each value).

However, Helen code may also work. Just share this code to show my approach to MID :)

 Sub MidArray() Dim ArrValues(3) As Variant Dim vValue As Variant Dim iCount As Integer ArrValues(0) = "Firstname=FIRSTNAME" ArrValues(1) = "Lastname=LASTNAME" ArrValues(2) = "Username=USERNAME" ArrValues(3) = "Displayname=DISPLAYNAME" For iCount = LBound(ArrValues) To UBound(ArrValues) ArrValues(iCount) = Mid(ArrValues(iCount), InStr(ArrValues(iCount), "=") + 1) Next iCount End Sub 
+3
source

To write back to an array, you need to use the notation ArrValues(index) = new_value . Thus, you need to use the usual For loop instead of For Each and access the elements of the array by their indices. Here's how you can do it:

 For i = LBound(ArrValues) To UBound(ArrValues) s = ArrValues(i) s = Split(s, "=") ArrValues(i) = s(1) Next 
+7
source

All Articles