Hey all,
To me, it should not be creating infinite loop. It loops through outer array, checking if the end characters of a string match whats in the end_string array. Then for that string that was cleaned, it goes through another loop to remove any characters that match the second array. That is, if the index of second array is at all present in the cleaner string, then those characters should be removed from inner string. Then it outputs results in column B. Problem is when I run this macro, it causes excel to freeze and I have to force quit excel:
Force quitting excel is not the behavior I want. I was hoping it would convert this:Sub StringChecker() Dim string_arr() As Variant Dim k As Integer Dim c As Range Set c = ActiveSheet.[A1] end_string = Array(" &", _ " TR", _ " SR", _ " DEFEN") substring = Array(" JR ", _ " SR ") Do While c <> "End Loop" c.Offset(0, 1) = c For k = 0 To UBound(end_string) If Right(c, Len(end_string(k))) = end_string(k) Then cleaner_string = Mid(c, 1, Len(c) - Len(end_string(k))) For l = 0 To UBound(substring) clean_string = Replace(cleaner_string, end_string(l), "") Next l c.Offset(0, 1) = clean_string End If Next k Set c = c.Offset(1, 0) Loop End Sub
john smith JR & jennifer TR
to:
john smith & jennifer
thanks for response
Last edited by johnmerlino; 07-18-2011 at 04:45 PM.
Do you have the string "End Loop" somewhere in column A? It is probably better to look for a blank cell (like the code I posted to solve this problem for you in another thread), rather than an arbitrary string. You are guaranteed to hit a blank cell eventually.
Making the world a better place one fret at a time
||||||
If someone helped you, please click on the star icon at the bottom of their post
If your problem is solved, please update the first post:
EDIT, Go Advanced button, set Prefix to SOLVED
[code]
' Enclose code in tags like this
[/code]
Don't attach a screenshot--just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.
Problem with what you provided was if there was an empty cell in cell a, then the loop ends. But even if there is empty cell in cell a, the next one might not be empty, so I wouldnt want the loop to just end. but actually I did forget to put the string "End Loop" and now the loop does end, but the inner loop does not remove the string " JR " or " SR " if its in cell, even though the REPLACE function should be doing that.
Thanks for response
Last edited by johnmerlino; 07-18-2011 at 05:20 PM.
I don't understand how you are using the variable substring. You are using its array size for your inner For loop, but you never reference the array anywhere in your code.
I suspect that
' this line clean_string = Replace(cleaner_string, end_string(l), "") ' should be changed to this clean_string = Replace(cleaner_string, substring(l), "")
Making the world a better place one fret at a time
||||||
If someone helped you, please click on the star icon at the bottom of their post
If your problem is solved, please update the first post:
EDIT, Go Advanced button, set Prefix to SOLVED
[code]
' Enclose code in tags like this
[/code]
Don't attach a screenshot--just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.
Thanks for response.
I apply this:
And for the substring array, it only ever applies the last index. It's as if the indexes before the last get overwritten or something. For example, if JR is last index, then it will only remove JR from string. If SR is last index, then it will only remove SR from string.Sub StringChecker() Dim string_arr() As Variant Dim k As Integer Dim c As Range Set c = ActiveSheet.[A1] end_string = Array(" &", _ " TR", _ " SR", _ " DEFEN") substring = Array(" SR ", _ " JR ") Do While c <> "End Loop" c.Offset(0, 1) = c For k = 0 To UBound(end_string) If Right(c, Len(end_string(k))) = end_string(k) Then cleaner_string = Mid(c, 1, Len(c) - Len(end_string(k))) For l = 0 To UBound(substring) clean_string = Replace(cleaner_string, substring(l), " ") Next l c.Offset(0, 1) = clean_string End If Next k Set c = c.Offset(1, 0) Loop End Sub
Thanks for response.
hmm is this also going on here?
http://www.mrexcel.com/forum/showthread.php?t=565477
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and a dabbler in Cisco
where does code go ?
look here
how to insert code
how to enter array formula
why use -- in sumproduct
recommended reading
wiki Mojito
how to say no convincingly
most important thing you need
Martin Wilson: SPV
and RSMBC
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks