Macro to auto replace name

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
626
Office Version
  1. 2016
Platform
  1. Windows
Hello, I'm needing help with getting a macro that will auto move Names from Range(N6:N140) to Replace names in Column C or K that corresponds with the names in Range(P6:P140). The twist is using the values in Range (O6:O140) to determine if the name needs to be replaced and/or if it needs to replace in the value In Column C or K. Only replace the values if cell in Column O starts with "STW" or "TTW" and not "PSTW". Replace the value in C column if the Start and end time match of the O column.

Ex. using the example below. using the table Range N6:P148 going down the list BILL replaces MICHAEL in column K, JOHN replaces TERRY in column K....ETC. If the name appears Once in column C & K then its a simple replace name in N to where the name in P appear in C or K. for the ex. PHILLIP replaces JASON in column C and not in column K because CELL O10 = "STW 0500-1330" and not "STW 1600-2000". and DANNY does not get replaced because value O12 = "PSTW 0500-1000" which is not a full shift. see final product below this table

Book1.xlsx
BCDEFGHIJKLMNOP
4DAN5:00-13:3013:00-21:30ROBERTSHIFT TRADES
5TOM5:00-13:3013:00-21:30MICHAELNAMETIME (TTW, PSTW)FOR
6ROB5:00-13:3013:00-21:30DAVIDBILLSTW 1300-2130MICHAEL
7MIKE5:45-14:1516:00-20:00PETERJOHNSTW 1300-2130TERRY
8RACHELSTW 1330-2200MILISSA
9JEN5:00-13:3013:00-21:30HARRYCORYSTW 1600-2000KOBY
10PAGE5:00-13:3013:00-21:30TERRYPHILLIPSTW 0500-1330JASON
11TED5:00-13:3013:00-21:30CLIFFORDDEREKSTW 0500-1330JEN
12ASHLEY5:45-14:1516:00-20:00JASONBOBPSTW 0500-1000DANNY
13
14DANNY5:00-13:3013:00-21:30BRIAN
15SARAH5:00-13:3013:00-21:30THOMAS
16
17LISA5:30-14:0014:00-22:30MARY
18
19JASON5:00-13:3013:00-21:30ALFORD
20ERIC5:00-13:3013:30-22:00MILISSA
21JORDAN5:00-13:3013:00-21:30CRYSTAL
22VICTORA5:45-14:1514:00-22:30MONDAY
23SMITH5:45-14:1514:00-22:30APRIL
24KYLE5:45-14:1516:00-20:00KOBY
BASE SCH
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C4:C24,K4:K24,P6:P21Cell ValueduplicatestextNO


Result below:

Book1.xlsx
BCDEFGHIJKLMNOP
4DAN5:00-13:3013:00-21:30ROBERTSHIFT TRADES
5TOM5:00-13:3013:00-21:30BILLNAMETIME (TTW, PSTW)FOR
6ROB5:00-13:3013:00-21:30DAVIDBILLSTW 1300-2130MICHAEL
7MIKE5:45-14:1516:00-20:00PETERJOHNSTW 1300-2130TERRY
8RACHELSTW 1330-2200MILISSA
9DEREK5:00-13:3013:00-21:30HARRYCORYSTW 1600-2000KOBY
10PAGE5:00-13:3013:00-21:30JOHNPHILLIPSTW 0500-1330JASON
11TED5:00-13:3013:00-21:30CLIFFORDDEREKSTW 0500-1330JEN
12ASHLEY5:45-14:1516:00-20:00JASONBOBPSTW 0500-1000DANNY
13
14DANNY5:00-13:3013:00-21:30BRIAN
15SARAH5:00-13:3013:00-21:30THOMAS
16
17LISA5:30-14:0014:00-22:30MARY
18
19PHILLIP5:00-13:3013:00-21:30ALFORD
20ERIC5:00-13:3013:30-22:00RACHEL
21JORDAN5:00-13:3013:00-21:30CRYSTAL
22VICTORA5:45-14:1514:00-22:30MONDAY
23SMITH5:45-14:1514:00-22:30APRIL
24KYLE5:45-14:1516:00-20:00CORY
BASE SCH


The yellow Highlights of the Manual Changes I need the Macro to accomplish. any help is greatly appreciated.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Give this code a try. It also highlights the updated cells.

VBA Code:
Sub comrepl()
Dim i, j, lrn, lrb As Long

lrn = Cells(Rows.Count, "O").End(xlUp).Row
lrb = Cells(Rows.Count, "B").End(xlUp).Row

For i = 1 To lrb
    For j = 1 To lrn
    If Cells(i, "B").Value = Cells(j, "O").Value And Left(Cells(j, "N").Value, 1) = "S" Then
        Cells(i, "B").Value = Cells(j, "M").Value
        Cells(i, "B").Interior.ColorIndex = 6 ' Remove line to stop updating cells
    End If
    If Cells(i, "J").Value = Cells(j, "O").Value And Left(Cells(j, "N").Value, 1) = "S" Then
        Cells(i, "J").Value = Cells(j, "M").Value
        Cells(i, "J").Interior.ColorIndex = 6 ' Remove line to stop updating cells
    End If
    Next j
Next i
End Sub
 
Upvote 0
Solution
Thank you ManiacB this was awesome. I did have to change most Column reference to match what I have but overall it was great.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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