Updating Multiple Columns Based on Cell Value

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
701
Office Version
  1. 365
Platform
  1. Windows
I'm curious, is there a "smarter" way to code this? It works, but it's lengthy so I'm wondering if there's a better way.

VBA Code:
For i = 2 To WDLR
    If Range("AP" & i).Value = "DS | Purple | N" Then
        Range("AQ" & i).Value = "N/A"
        Range("AR" & i).Value = "2"
        Range("AS" & i).Value = "N3"
        Range("AT" & i).Value = "Y"
        Range("AU" & i).Value = "Y"
        Range("AV" & i).Value = Range("J" & i).Value
        Range("AW" & i).Value = "N/A"
        Range("AX" & i).Value = "N/A"
        Range("AY" & i).Value = "E"
        Range("AZ" & i).Value = "E"
    ElseIf Range("AP" & i).Value = "DS | Purple | Y" Then
        Range("AQ" & i).Value = "N/A"
        Range("AR" & i).Value = "0"
        Range("AS" & i).Value = "I3"
        Range("AT" & i).Value = "NULL"
        Range("AU" & i).Value = "R"
        Range("AV" & i).Value = Range("J" & i).Value
        Range("AW" & i).Value = "N/A"
        Range("AX" & i).Value = "N/A"
        Range("AY" & i).Value = "No Update"
        Range("AZ" & i).Value = "No Update"
    ElseIf Range("AP" & i).Value = "DS | Green | N" Then
        Range("AQ" & i).Value = "0"
        Range("AR" & i).Value = "2"
        Range("AS" & i).Value = "N3"
        Range("AT" & i).Value = "Y"
        Range("AU" & i).Value = "N/A"
        Range("AV" & i).Value = "N/A"
        Range("AW" & i).Value = "11"
        Range("AX" & i).Value = Range("J" & i).Value
        Range("AY" & i).Value = "F"
        Range("AZ" & i).Value = "F"
    ElseIf Range("AP" & i).Value = "DS | Green | Y" Then
        Range("AQ" & i).Value = "0"
        Range("AR" & i).Value = "0"
        Range("AS" & i).Value = "I3"
        Range("AT" & i).Value = "NULL"
        Range("AU" & i).Value = "N/A"
        Range("AV" & i).Value = "N/A"
        Range("AW" & i).Value = "11"
        Range("AX" & i).Value = Range("J" & i).Value
        Range("AY" & i).Value = "No Update"
        Range("AZ" & i).Value = "No Update"
    ElseIf Range("AP" & i).Value = "DS | Yellow | N" Then
        Range("AQ" & i).Value = "0"
        Range("AR" & i).Value = "2"
        Range("AS" & i).Value = "N3"
        Range("AT" & i).Value = "Y"
        Range("AU" & i).Value = "N/A"
        Range("AV" & i).Value = "N/A"
        Range("AW" & i).Value = "12"
        Range("AX" & i).Value = Range("J" & i).Value
        Range("AY" & i).Value = "G"
        Range("AZ" & i).Value = "G"
    ElseIf Range("AP" & i).Value = "DS | Yellow | Y" Then
        Range("AQ" & i).Value = "0"
        Range("AR" & i).Value = "0"
        Range("AS" & i).Value = "I3"
        Range("AT" & i).Value = "NULL"
        Range("AU" & i).Value = "N/A"
        Range("AV" & i).Value = "N/A"
        Range("AW" & i).Value = "12"
        Range("AX" & i).Value = Range("J" & i).Value
        Range("AY" & i).Value = "No Update"
        Range("AZ" & i).Value = "No Update"
    ElseIf Range("AP" & i).Value = "DS | Blue | N" Then
        Range("AQ" & i).Value = "0"
        Range("AR" & i).Value = "2"
        Range("AS" & i).Value = "N3"
        Range("AT" & i).Value = "Y"
        Range("AU" & i).Value = "N/A"
        Range("AV" & i).Value = "N/A"
        Range("AW" & i).Value = "13"
        Range("AX" & i).Value = Range("J" & i).Value
        Range("AY" & i).Value = "H"
        Range("AZ" & i).Value = "H"
    ElseIf Range("AP" & i).Value = "DS | Blue | Y" Then
        Range("AQ" & i).Value = "0"
        Range("AR" & i).Value = "0"
        Range("AS" & i).Value = "I3"
        Range("AT" & i).Value = "NULL"
        Range("AU" & i).Value = "N/A"
        Range("AV" & i).Value = "N/A"
        Range("AW" & i).Value = "13"
        Range("AX" & i).Value = Range("J" & i).Value
        Range("AY" & i).Value = "No Update"
        Range("AZ" & i).Value = "No Update"
    End If
Next i
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You can put all the values on a "helper" sheet. For example:

Dante Amor
ABCDEFGHIJK
1TEXTVALUE1VALUE2VALUE3VALUE4VALUE5VALUE6VALUE7VALUE8VALUE9VALUE10
2DS | Purple | NN/A2N3YYRange("J" & i)N/AN/AEE
3DS | Purple | YN/A0I3NULLRRange("J" & i)N/AN/ANo UpdateNo Update
4DS | Green | N02N3YN/AN/A11Range("J" & i)FF
5DS | Green | Y00I3NULLN/AN/A11Range("J" & i)No UpdateNo Update
6DS | Yellow | N02N3YN/AN/A12Range("J" & i)GG
7DS | Yellow | Y00I3NULLN/AN/A12Range("J" & i)No UpdateNo Update
8DS | Blue | N02N3YN/AN/A13Range("J" & i)HH
9DS | Blue | Y00I3NULLN/AN/A13Range("J" & i)No UpdateNo Update
aux

Run the following code on the sheet where you are going to update the information.

VBA Code:
Sub code_2()
  Dim i As Long
  Dim sh As Worksheet
  Dim f As Range, r As Range
  Dim col As String
 
  Set sh = Sheets("aux")
  For i = 2 To Range("AP" & Rows.Count).End(3).Row
    Set f = sh.Range("A:A").Find(Range("AP" & i).Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      With Range("AQ" & i).Resize(1, 10)
        .Value = f.Offset(0, 1).Resize(1, 10).Value
        Set r = .Find("Range", , xlValues, xlPart, , , False)
        If Not r Is Nothing Then
          col = Split(r.Value, """")(1)
          r.Value = Range(col & i).Value
        End If
      End With
    End If
  Next
End Sub

This way you can add colors in the "aux" sheet and the macro will remain the same.
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,012
Members
449,280
Latest member
Miahr

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