VBA find and replace under multiple conditions

chris54

New Member
Joined
Aug 23, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I want to create a macro where I can find and replace the wording of my data in one column depending on the condition of another.

I would like to search column A for "Scotland" and "Southern" then if column J is "SHIPPER" change column A to "Scotland Transport" or "Southern Transport"

Is anyone able to suggest a way to do this? I've been tinkering but anything I try seems to fail
 

Attachments

  • EXAMPLE.JPG
    EXAMPLE.JPG
    93 KB · Views: 21

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Sub Replace_data1()
'
' Replace_data1 Macro
'

'

Sheets("TEST").Select
Dim LastRow As Long
Dim i As Long
LastRow = Range("J" & Rows.Count).End(xlUp).Row


For i = 1 To LastRow
If Range("J" & i).Value = "SHIPPER" & Range("A" & i).Value = "Scotland" Then
Range("A" & i).Value = "Scotland TRANSPORT"
End If
Next i

End Sub

This is what I have so far
 

Attachments

  • EXAMPLE.JPG
    EXAMPLE.JPG
    35.1 KB · Views: 11
Upvote 0
Try:
VBA Code:
Sub chris54()
'I would like to search column A for "Scotland" and "Southern" then if column J is "SHIPPER"
'change column A to "Scotland Transport" or "Southern Transport"

Dim c As Range, x
Dim sAddress As String
Application.ScreenUpdating = False
For Each x In Array("Scotland", "Southern")
    With Range("A:A")
    Set c = .Find(What:=x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    
    If Not c Is Nothing Then
        sAddress = c.Address
        Do
           Set c = .FindNext(c)
           If Cells(c.Row, "J") = "SHIPPER" Then
                c = x & " Transport"
           End If
        Loop While Not c Is Nothing And c.Address <> sAddress
    End If
    End With
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Another way to try:
VBA Code:
Sub chris54_A()
Dim n As Long, i As Long
Dim va, vb
n = Range("A" & Rows.Count).End(xlUp).Row
va = Range("A1:A" & n)
vb = Range("J1:J" & n)

For i = 2 To UBound(va, 1) 'data start at row 2
    If vb(i, 1) = "SHIPPER" Then
        If va(i, 1) = "Scotland" Then
            va(i, 1) = "Scotland Transport"
        ElseIf va(i, 1) = "Southern" Then
            va(i, 1) = "Southern Transport"
        End If
    End If
Next

Range("A1").Resize(UBound(va, 1), 1) = va
End Sub
 
Upvote 0
Try:
VBA Code:
Sub chris54()
'I would like to search column A for "Scotland" and "Southern" then if column J is "SHIPPER"
'change column A to "Scotland Transport" or "Southern Transport"

Dim c As Range, x
Dim sAddress As String
Application.ScreenUpdating = False
For Each x In Array("Scotland", "Southern")
    With Range("A:A")
    Set c = .Find(What:=x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
   
    If Not c Is Nothing Then
        sAddress = c.Address
        Do
           Set c = .FindNext(c)
           If Cells(c.Row, "J") = "SHIPPER" Then
                c = x & " Transport"
           End If
        Loop While Not c Is Nothing And c.Address <> sAddress
    End If
    End With
Next
Application.ScreenUpdating = True
End Sub
LEGEND!! Thank you for the help!!!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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