VBA Code - If statement to find exact match between columns as well as if value exists in another column

MHamid

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

Need some assistance, I have the below code, but I'm trying to figure out two things. 1) If cell value in column BE = cell value in column BG, then cell value in column BG should be replaced with blank. 2) If cell value in column BE is included within the text string in column BG, then replace that value in string only to "".
BG being J in the code and BE being K in the code (saved ranges within the code).

Full Code:
VBA Code:
Sub CleanUpBusImp()

Dim sh As Worksheet
Dim lr As Long
Dim i&, rngF, rngJ, rngK, F As String, J As String, K As String, CleanBusImp()
Set sh = Sheets("Main")
lr = sh.Range("A" & Rows.Count).End(xlUp).Row
rngF = Range("BD3:BD" & lr).Value: rngJ = Range("BG3:BG" & lr).Value: rngK = Range("BE3:BE" & lr).Value
ReDim CleanBusImp(1 To UBound(rngJ), 1 To 1)
    For i = 1 To UBound(rngJ)
        F = rngF(i, 1): J = rngJ(i, 1): K = rngK(i, 1)
        Select Case True
            Case (F Like "Shared Non-O&T")
            'Case (F <> "Business")
                    CleanBusImp(i, 1) = Replace(Replace(Replace(Replace(Replace(Replace(Replace(J, "/EO&T", ""), "/LF - PBWM O&T", ""), "LF - PBWM O&T/", ""), "/PBWM O&T", ""), "/ICG O&T", ""), "PBWM O&T/", ""), "EO&T/", "")
                If J = "LF - PBWM O&T" Then
                    CleanBusImp(i, 1) = "LF - PBWM Operations/LF - PBWM Technology"
                ElseIf J = "PBWM O&T" Then
                    CleanBusImp(i, 1) = "PBWM Operations/PBWM Technology"
                ElseIf J = "ICG O&T" Then
                    CleanBusImp(i, 1) = "ICG Operations/ICG Technology"
                End If
            Case (F <> "Business") And (F <> "Shared Non-O&T")
                    CleanBusImp(i, 1) = Replace(Replace(Replace(Replace(Replace(Replace(Replace(J, "/EO&T", ""), "/LF - PBWM O&T", ""), "LF - PBWM O&T/", ""), "/PBWM O&T", ""), "/ICG O&T", ""), "PBWM O&T/", ""), "EO&T/", "")
                If K = J Then
                    'CleanBusImp(i, 1) = ""
                    J = ""
                End If
    
        End Select
    
    Next

With Range("BG3").Resize(UBound(rngJ), 1)
    .ClearContents
    .Value = CleanBusImp
End With


End Sub


Below is the part of the code I'm trying to solve for ... if BE = BG, then BG should be blank and then add another condition where if value in BE is included in column BG, then the value form BE should be removed from column BG. However, the code I have now is replacing every value in column BG as blanks. No sure what I am missing or doing wrong here. Provided a table below with a small sample data.

VBA Code:
            Case (F <> "Business") And (F <> "Shared Non-O&T")
                    CleanBusImp(i, 1) = Replace(Replace(Replace(Replace(Replace(Replace(Replace(J, "/EO&T", ""), "/LF - PBWM O&T", ""), "LF - PBWM O&T/", ""), "/PBWM O&T", ""), "/ICG O&T", ""), "PBWM O&T/", ""), "EO&T/", "")
                If K = J Then
                    'CleanBusImp(i, 1) = ""
                    J = ""
                End If


L2_BusinessBusiness_ImpactedResult
Business 1Business 1/Business 4Business 4
Business 1Business 1
Business 2Business 2/Business 3/Business 5Business 3/Business 5
Business 1Business 1
Business 2Business 4/Business 3/Business 2/Business 5Business 4/Business 3/Business 5


Thank you,
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,216,063
Messages
6,128,552
Members
449,458
Latest member
gillmit

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