Find and replace under conditions using VBA

chris54

New Member
Joined
Aug 23, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi there,
I'm trying to create a macro in VBA to search through large amounts of data and change the wording depending on the condition.

I want to search column J for "SHIP" and if so, change column A to "TRANSPORT".

I've been fiddling with the following but can't get it working.

Can anyone help?


Dim x As Integer
Dim i As Integer

x = Range("J" & Rows.Count).End(xlUp).Row

For i = 1 To x

If Range("J" & i).Value = "SHIPPER" Then
Range("A" & i).Value = "TRANSPORT"
End If
Next i
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Welcome to the Board!

I want to search column J for "SHIP"

If Range("J" & i).Value = "SHIPPER" Then
Which is it? Are you checking for "SHIP" or "SHIPPER"?
Is that the issue?

Also, are the values in column J EXACTLY equal to the value you are looking for, with no other characters (including) spaces in those cells?
Note that something as simple as an extra space will cause it NOT to match.

If you still cannot figure it out, please post a small sample of your data.
 
Upvote 0
Welcome to the Board!




Which is it? Are you checking for "SHIP" or "SHIPPER"?
Is that the issue?

Also, are the values in column J EXACTLY equal to the value you are looking for, with no other characters (including) spaces in those cells?
Note that something as simple as an extra space will cause it NOT to match.

If you still cannot figure it out, please post a small sample of your data.
Hi,

Sorry I'm searching for "SHIPPER" in Column J and want to change the corresponding rows to be "TRANSPORT" in column A. The values in column J are exactly equal to what I'm looking for but I'm relatively new to VBA so struggling to perfect it.
 

Attachments

  • EXAMPLE.JPG
    EXAMPLE.JPG
    67.8 KB · Views: 9
Upvote 0
Your code works perfectly fine for me.

What is the name of the module have you placed this code?

If you place this formula in cell K4, what does it return?
Excel Formula:
=LEN(J4)
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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