Excel VBA Code to Update Existing Value in Column based on value in another column

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I need some help figuring out how to update the below code to update existing values in column AR based on if specific text exists in column AP.
I'm pretty sure I will need to remove the "ClearContents" line, but not sure what I will need to do and was confused with what I found online.
I can provide sample data if needed, but a preliminary push to in the right direction would help.

VBA Code:
Sub RemoveOT_Tagging()

Dim sh As Worksheet
Dim lr As Long
Dim i&, rngZ, rngK, rngE, Z As String, K As String, E As String, RemoveTagging()
Set sh = Sheets("Audit_Plan")
lr = sh.Range("J" & Rows.Count).End(xlUp).Row
rngZ = sh.Range("AR7:AR" & lr).Value: rngK = sh.Range("AP7:AP" & lr).Value: rngE = sh.Range("AN7:AN" & lr).Value
ReDim CleanBusImp(1 To UBound(rngZ), 1 To 1)
    For i = 1 To UBound(rngZ)
        Z = rngZ(i, 1): K = rngK(i, 1): E = rngE(i, 1)
        
        Select Case True
            Case (E Like "O&T Area")
            'Code to remove ICG O&T, PBWM O&T and LF - PBWM O&T of there is no indication drilled down
                If (K = "ICG Operations") And (Z Like "*ICG O&T*") Then
                    RemoveTagging(i, 1) = Replace(Replace(Replace(Z, "/ICG O&T", ""), "ICG O&T/", ""), "ICG O&T", "")
                ElseIf (K = "ICG Technology") And (Z Like "*ICG O&T*") Then
                    RemoveTagging(i, 1) = Replace(Replace(Replace(Z, "/ICG O&T", ""), "ICG O&T/", ""), "ICG O&T", "")
                ElseIf (K = "LF - PBWM Operations") And (Z Like "*LF - PBWM O&T*") Then
                    RemoveTagging(i, 1) = Replace(Replace(Replace(Z, "/LF - PBWM O&T", ""), "LF - PBWM O&T/", ""), "LF - PBWM O&T", "")
                ElseIf (K = "LF - PBWM Technology") And (Z Like "*PBWM O&T*") Then
                    RemoveTagging(i, 1) = Replace(Replace(Replace(Z, "/LF - PBWM O&T", ""), "LF - PBWM O&T/", ""), "LF - PBWM O&T", "")
                ElseIf (K = "PBWM Operations") And (Z Like "*PBWM O&T*") Then
                    RemoveTagging(i, 1) = Replace(Replace(Replace(Z, "/PBWM O&T", ""), "PBWM O&T/", ""), "PBWM O&T", "")
                ElseIf (K = "PBWM Technology") And (Z Like "*PBWM O&T*") Then
                    RemoveTagging(i, 1) = Replace(Replace(Replace(Z, "/PBWM O&T", ""), "PBWM O&T/", ""), "PBWM O&T", "")
                End If
        End Select
    Next

With sh.Range("AR7").Resize(UBound(rngZ), 1)
    .ClearContents
    .Value = RemoveTagging
End With


End Sub


Thank you,
 

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
Hi @MHamid . Thanks for posting on the forum.

I have a couple of observations:

1. The first condition that must be fulfilled is that cell AN has this text: "O&T Area", but I suppose it must be between asterisks:​
Rich (BB code):
Case (E Like "*O&T Area*")

2. Continuing with the pattern of the conditions, here you are missing the letters "LF -"​
Rich (BB code):
 ElseIf (K = "LF - PBWM Operations") And (Z Like "*LF - PBWM O&T*") Then
   RemoveTagging(i, 1) = Replace(Replace(Replace(Z, "/LF - PBWM O&T", ""), "LF - PBWM O&T/", ""), "LF - PBWM O&T", "")
ElseIf (K = "LF - PBWM Technology") And (Z Like "*LF - PBWM O&T*") Then

-------------------------​
I tried to simplify the code. Please try the following:
VBA Code:
Sub RemoveOT_Tagging_v1()
  Dim sh As Worksheet
  Dim i As Long, j As Long
  Dim a As Variant, b As Variant, ar1 As Variant, ar2 As Variant
 
  Set sh = Sheets("Audit_Plan")
  a = sh.Range("AN7:AR" & sh.Range("AR" & Rows.Count).End(xlUp).Row).Value
  ReDim b(1 To UBound(a), 1 To 1)
 
  ar1 = Array("ICG Operations", "ICG Technology", "LF - PBWM Operations", "LF - PBWM Technology", "PBWM Operations", "PBWM Technology")
  ar2 = Array("ICG O&T", "ICG O&T", "LF - PBWM O&T", "LF - PBWM O&T", "PBWM O&T", "PBWM O&T")

  For i = 1 To UBound(a)
    If a(i, 1) Like "*O&T Area*" Then
      For j = 0 To UBound(ar1)
        If a(i, 3) = ar1(j) And a(i, 5) Like "*" & ar2(j) & "*" Then
          b(i, 1) = Replace(Replace(Replace(a(i, 5), "/" & ar2(j), ""), ar2(j) & "/", ""), ar2(j), "")
        End If
      Next
    End If
  Next
 
  sh.Range("AR7").Resize(UBound(b)).Value = b
End Sub

I tried with the following:
Dante Amor
ANAPAR
6EKZ
7O&T AreaICG Operationsini /ICG O&T fin ICG O&T
8O&T AreaICG Technologyini ICG O&T/ fin
9O&T AreaLF - PBWM Operationsini /LF - PBWM O&T fin
10O&T AreaLF - PBWM Technologyini /LF - PBWM O&T fin
11O&T AreaPBWM Operationsini /PBWM O&T fin
12O&T AreaPBWM Technologyini PBWM O&T/ fin
Audit_Plan

The result:
Dante Amor
ANAPAR
6EKZ
7O&T AreaICG Operationsini fin
8O&T AreaICG Technologyini fin
9O&T AreaLF - PBWM Operationsini fin
10O&T AreaLF - PBWM Technologyini fin
11O&T AreaPBWM Operationsini fin
12O&T AreaPBWM Technologyini fin
Audit_Plan


Note:
If the result is not what you are looking for, then give an example of your data and the expected result. Use XL2BB tool.

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,126
Members
453,021
Latest member
Justyna P

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