Inner Loop not working correctly

johnmerlino

Board Regular
Joined
Sep 21, 2010
Messages
94
Hey all,

I would like to convert this:

abc SR abc &
abc JR abc TR

to this:

abc abc
abc abc

In this code:

Code:
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)))
        
      End If
      
    
   Next k
   For l = 0 To UBound(substring)
        clean_string = Replace(cleaner_string, substring(l), " ")
         
    Next l
        
    c.Offset(0, 1) = clean_string
    
   Set c = c.Offset(1, 0)

Loop

End Sub

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.

Thanks for response.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I am still having a problem with this:

Code:
Sub StringChecker()
Dim string_arr() As Variant
Dim k As Integer
Dim c As Range
Set c = ActiveSheet.[A1]
 
end_string = Array(" &"," TRS"," TRUST", " JTRS")
substring = Array(" SR ", " II ", " LE ")
 
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
   c.Offset(0, 1) = c
   cleaner_string = ""
      
   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)))
      End If
   Next k
   
   If cleaner_string = "" Then
      clean_string = c  
   Else
      clean_string = cleaner_string  
   End If
   
   
   For l = 0 To UBound(substring)
        clean_string = Replace(clean_string, substring(l), " ")
    Next l
    
    
    c.Offset(0, 1) = clean_string
   Set c = c.Offset(1, 0)
 
Next c
End Sub


For example this name:

abc a def TRUST &

will return this:

abc a def TRUST

even though it should return this:

abc a def

since both " &" and " TRUST" are in the end_string array. It seems to only do one pass by, even though it's clearly in a loop and should clean out all indices of that loop. Not sure what's going on.

Thanks for response.
 
Upvote 0
in this loop:
Code:
   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)))
      End If
   Next k
You are always operating on c, not the progressively-trimmed result. Step through the code.
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,652
Members
452,934
Latest member
mm1t1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top