change cell value depending on current value

ghynes

Board Regular
Joined
Dec 21, 2004
Messages
96
i have a multiple rows of data and i need to loop through every B cell and change the value depending on the following criteria. can anyone help with a simple macro code for this?
thanks

if B1 = car1 rename to Toyota
if B1 = car2 rename to Ford
if B1 = car3 rename to Opel
if B1 = car4 rename to Nissan

and then i need to loop through every B cell until the end of the rows.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Looping is a relatively slow process. See if this does what you want.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Replace_Text()<br>    <SPAN style="color:#00007F">Dim</SPAN> Cars<br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    Cars = Array("car1", "Toyota", "car2", "Ford", "car3", "Opel", "car4", "Nissan")<br>    <SPAN style="color:#00007F">With</SPAN> Columns("B")<br>        <SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(Cars) - 1 <SPAN style="color:#00007F">Step</SPAN> 2<br>            .Replace What:=Cars(i), Replacement:=Cars(i + 1), LookAt:=xlWhole, _<br>                MatchCase:=False, SearchFormat:=<SPAN style="color:#00007F">False</SPAN>, ReplaceFormat:=False<br>        <SPAN style="color:#00007F">Next</SPAN> i<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
thanks for the help. i think i mite use the following code;

Code:
Set myrange = range("B1:B100")
 
    For Each Cell In myrange
    
    If Cell.Value = "car1" Then
       Cell.Value = "Toyota"
    End If
        
    If Cell.Value = "car2" Then
       Cell.Value = "Ford"
    End If

    If Cell.Value = "car3" Then
       Cell.Value = "Opel"
    End If
       If Cell.Value = "car4" Then
       Cell.Value = "Nissan"
    End If

Next 
End Sub

but i need help with determining the range as the number of rows change every day.
do you know of a way i can set the range to the exact number of rows in the data?
 
Upvote 0
think i figured out how to do this.

Code:
Dim myrange As range

Lastrow = Cells(Cells.Rows.Count, "B").End(xlUp).Row

Set myrange = range("B1:B" & Lastrow)
    For Each Cell In myrange

seems to work. hoping its right anyhow
 
Upvote 0
Say you have 100 rows. Your code will have to do 400 processes. Did you try my code that will need just 4? Can't really see why you would deliberately choose a method that requires 100 times the work! :confused:
 
Upvote 0

Forum statistics

Threads
1,203,455
Messages
6,055,541
Members
444,794
Latest member
HSAL

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