aaronk Well-Known Member Licensed User Longtime User Jan 10, 2015 #1 Hi, In my Globals sub I have declared a string like: Code: Copy Dim MyString(200) as String I am then setting the string from another sub like: Code: Copy MyString(1) = "one" MyString(2) = "two" MyString(3) = "three" If I want to clear all the strings, what would be the best way in doing that? Would I need to use a For loop like below or is there a better faster way? Code: Copy For i = 1 to 200 MyString(i) = "" Next
Hi, In my Globals sub I have declared a string like: Code: Copy Dim MyString(200) as String I am then setting the string from another sub like: Code: Copy MyString(1) = "one" MyString(2) = "two" MyString(3) = "three" If I want to clear all the strings, what would be the best way in doing that? Would I need to use a For loop like below or is there a better faster way? Code: Copy For i = 1 to 200 MyString(i) = "" Next
udg Expert Licensed User Longtime User Jan 10, 2015 #2 The fastest way should be to dim the array again, so: Code: Copy Dim MyStrings(200) as String Upvote 0
klaus Expert Licensed User Longtime User Jan 10, 2015 #3 Be careful: With Dim MyString(200) As String and For i = 1 To 200 you will get an error ! You should use either For i = 0 To 199 or Dim MyString(201) As String if you really want the indexes from 1 to 200, the index 0 wouldn't be used in this case. Upvote 0
Be careful: With Dim MyString(200) As String and For i = 1 To 200 you will get an error ! You should use either For i = 0 To 199 or Dim MyString(201) As String if you really want the indexes from 1 to 200, the index 0 wouldn't be used in this case.
aaronk Well-Known Member Licensed User Longtime User Jan 10, 2015 #4 udg said: The fastest way should be to dim the array again, so: Code: Copy Dim MyStrings(200) as String Click to expand... But How? I am Dimming MyString in the Globals sub. If I was to re-dim the MyString it would only be accessible from that sub and not the rest of my code. @klaus The code I provided was only a example so people know what I am trying to do.. I actually have it set as what you said. (I had made a mistake with the code I posted.. Whops..) I will use the For loop to clear it as I think this is the only way in clearing the strings. Upvote 0
udg said: The fastest way should be to dim the array again, so: Code: Copy Dim MyStrings(200) as String Click to expand... But How? I am Dimming MyString in the Globals sub. If I was to re-dim the MyString it would only be accessible from that sub and not the rest of my code. @klaus The code I provided was only a example so people know what I am trying to do.. I actually have it set as what you said. (I had made a mistake with the code I posted.. Whops..) I will use the For loop to clear it as I think this is the only way in clearing the strings.
stevel05 Expert Licensed User Longtime User Jan 10, 2015 #5 If I was to re-dim the MyString it would only be accessible from that sub and not the rest of my code. Click to expand... This is not true, if you re-dim a global variable, it is still a global variable. Just reset. Upvote 0
If I was to re-dim the MyString it would only be accessible from that sub and not the rest of my code. Click to expand... This is not true, if you re-dim a global variable, it is still a global variable. Just reset.
aaronk Well-Known Member Licensed User Longtime User Jan 10, 2015 #6 stevel05 said: This is not true, if you re-dim a global variable, it is still a global variable. Just reset. Click to expand... Umm.. So you are right. I always thought if you dim something it was only accessible in that sub. But looks like if you dim it again in a sub, and it's in the Globals or Process_Globals subs it looks like it re-dims it again, so yes in my case clears the strings. You learn something all the time. Thanks for confirming this, and looks like I can just re-dim it again which will make it easy for me. Upvote 0
stevel05 said: This is not true, if you re-dim a global variable, it is still a global variable. Just reset. Click to expand... Umm.. So you are right. I always thought if you dim something it was only accessible in that sub. But looks like if you dim it again in a sub, and it's in the Globals or Process_Globals subs it looks like it re-dims it again, so yes in my case clears the strings. You learn something all the time. Thanks for confirming this, and looks like I can just re-dim it again which will make it easy for me.