For loop to rename cell

SCPbrito

New Member
Joined
Aug 1, 2024
Messages
43
Office Version
  1. 365
Platform
  1. Windows
How can I write a for loop to to go through cells in a column and looks for "Customer A" "Customer B" and renames those to "Customer C"
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Do you need to loop through each cell in the column? Couldn't you just do two Find and Replace on the whole column?
 
Upvote 0
Im new to Excel so idk. Id like to learn the loop to use it for other things. But i guess in this scenario the other method would be more effiecient
 
Upvote 0
I tried using this code from something else but it doesnt seem to work as intended.


Rich (BB code):
Dim i As Long
  
  Application.ScreenUpdating = False
  For i = 1 To Range("I" & Rows.Count).End(3).Row
    Select Case Right(Range("H" & i).Value, 1)
      Case "Balaji Trading Inc -Cricket": Range("I" & i).Value = "Cricket"
      Case "Balaji Trading,Inc -Other": Range("I" & i).Value = "Cricket"
    End Select
  Next
  Application.ScreenUpdating = True
 
Upvote 0
If you want a loop, this would do that.
It is probably the most inefficient way, but after you learn this, you can, as Peter suggested, look into different ways.

Code:
Sub LoopidieLoop()
Dim c As Range
    For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If c.Value = "Customer A" Or c.Value = "Customer B" Then c.Value = "Customer C"
    Next c
End Sub
 
Upvote 0
Peter will probaly show you the Find and Replace method but here is a way to do it within the computer's memory.
Code:
Sub Within_Computer_Memory()
Dim datArr, i As Long
datArr = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value
    For i = LBound(datArr) To UBound(datArr)
        If datArr(i, 1) = "Customer A" Or datArr(i, 1) = "Customer B" Then datArr(i, 1) = "Customer C"
    Next i
Cells(1).Resize(UBound(datArr)).Value = datArr
End Sub
 
Upvote 0
I tried using this code from something else but it doesnt seem to work as intended.
Clearly that code is to do a different job to the one you described in post #1. What is it that you are actually trying to do? As well as an explanation, what about a small set of sample data and the expected results with XL2BB?

(If you have trouble with XL2BB review the "XL2BB Icons greyed out" link in the 'Known XL2BB issues' section near the top of theXL2BB Instructions page linked above.)
 
Upvote 0
I managed to include the find and replace in my code. But when I execute the macro the find and replace isnt working.


Rich (BB code):
Dim i As Long
  
  Application.ScreenUpdating = False
  For i = 1 To Range("H" & Rows.Count).End(3).Row
    Select Case Left(Range("H" & i).Value, 1)
      Case "6": Range("I" & i).Value = "Cellphone Repair"
      Case "7": Range("I" & i).Value = "Metro PCS"
      Case "8": Range("I" & i).Value = "Cricket"
    End Select
  Next
  Application.ScreenUpdating = True
 Selection.Replace What:="Balaji Trading, Inc -Other", Replacement:= _
        "Cricket", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
        SearchFormat:=False, ReplaceFormat:=False, FormulaVersion:= _
        xlReplaceFormula2
    Selection.Replace What:="Balaji Trading Inc -Cricket", Replacement:= _
        "Cricket", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
        SearchFormat:=False, ReplaceFormat:=False, FormulaVersion:= _
        xlReplaceFormula2
End Sub
 
Upvote 0
It looks like it is running on the "Selected" range.
Is that being selected somewhere earlier in your code?

If not, it is probably running on whatever the selected range is when you started the code.
What range is that?

What range do you exactly want to run your Find/Replace on?

You seem to have a few things going on, and it is a bit confusing without seeing your data and a detailed explanation of exactly all that you are trying to do.
I think it would be VERY beneficial for us if you could do what Peter asked, and show us an example of your data and expected results.
 
Upvote 0
Here is a screenshot of the data. I need anything in Customer No that starts with a "6" renamed to "CellPhone Repair", anything with a "7" renamed to "Metro" and anything with an "8" renamed to "Cricket". Then i need anything in Customer name that is "Balaji Trading, Inc -Other" or "Balaji Trading Inc -Cricket" renamed to "Cricket"
 

Attachments

  • 2024-08-05_9-15-36.png
    2024-08-05_9-15-36.png
    34.2 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,221,577
Messages
6,160,609
Members
451,657
Latest member
Ang24

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