VBA to look and replace text in same row, different column

xChelly

New Member
Joined
Apr 10, 2018
Messages
2
I'm relatively new to VBA and I'm trying to write a code that will look at the receiving person column for Peter and will replace the Target Person and Target Fruit columns to match what's in the Sending Person and Sending Fruit.

I used this thread here to figure out the code, but that thread is looking values for different sheets. All of my data is in the same sheet, same rows, but different columns.


Raw Data:
Sending PersonSending FruitReceiving PersonReceiving FruitTarget PersonTarget Fruit
GeorgeStrawberriesPeterJackfruitAmyBlackberries
KelseyBlueberriesPeterJackfruitKeithGrapes

<tbody>
</tbody>

Should Be:
Sending PersonSending FruitReceiving PersonReceiving FruitTarget PersonTarget Fruit
GeorgeStrawberriesPeterJackfruitGeorgeStrawberries
KelseyBlueberriesPeterJackfruitKelseyBlueberries

<tbody>
</tbody>


Any help would be appreciated.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
In others words you want to replace last 2 columns by the 2 first one ??
 
Upvote 0
Try
Adjust "WkWd" to what you need

Code:
Sub Treat()
Const WkWd As String = "Peter"
Dim LR   As Integer, I  As Integer
    LR = Cells(Rows.Count, 1).End(3).Row
    For I = 2 To LR
        If (Cells(I, "C") = WkWd) Then
            Cells(I, "E") = Cells(I, "A")
            Cells(I, "F") = Cells(I, "B")
        End If
    Next
    MsgBox ("Job Done")
End Sub
 
Upvote 0
In others words you want to replace last 2 columns by the 2 first one ??

I tried to simplify the table, but it's actually something like this.

IDPurposeSending FnameSending LnameSending FruitReceiving FnameReceivingLNameReceiving FruitTarget PersonTarget Fruit
PDNINTCGeorgeSmithBerriesPeterXJackfruitKeithGrapes
SFOINTCKelseyDaApplesDaisyXXGrapesMichaelBerries
SEAINTCAmyBBananasPeterXJackfruitKannaDragonfruit

<colgroup><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>

But yeah that's essentially what I want to do with extra tables/data inbetween the other columns.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,746
Members
449,050
Latest member
excelknuckles

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