I have a very simple question for you, but I cannot find it on the internet. I have a list called ArrayList(i) of which I want to replace some strings for only one line. After that line, it should be changed back again. It works, but I want to learn how to do this more efficiently. I presume I need a loop only for the "fileName" line, but I'm not sure. The length of ArrayList is different everytime, and does not always contain the strings to be replaced, only somtimes.
'REPLACE NAME
ArrayList(i) = Replace(ArrayList(i), "NameOld1", "NameNew1")
ArrayList(i) = Replace(ArrayList(i), "NameOld2", "NameNew2")
finalName = PathCut & "XT\" & NumPart(initName) & "_" & partcode & " " & ArrayList(i) & " " & CodeNR & ExtNew
'REPLACE NAME BACK
ArrayList(i) = Replace(ArrayList(i), "NameNew1", "NameOld1")
ArrayList(i) = Replace(ArrayList(i), "NameNew2", "NameOld2")
swApp.CloseDoc ArrayList(i) & ".SLDPRT" 'Close the files
I am sure there must be an easier and cleaner way. Thank you in advance!
NumPart? When you say having a list, do you mean an array? If not sure, how does your code load it? If an array, you can replace at once, using the joined array. Something likeDim strArr as StringstrArr=join(ArrayList, "|")thenArrayList = split(Replace(Replace(strArr, "NameOld1", "NameNew1"), "NameOld2", "NameNew2"), "|"). Now you have the array with replaced strings. You can use a different array for that (arrRepl = ...). If the string to be replaced does not exist, nothing wrong happens.