Search for a specific value in two columns and return a single result

Sir_Calvin

New Member
Joined
Nov 16, 2022
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Right now, my code searched for "MI (D)" in column "B" and adds 4 additional lines at the bottom of the code dependent on what State is next to that value in column "A". However, there are some instances where there are many "MI (D)" combinations, so the code cycles through each one and adds dozens of lines of data. Is there a way to search for the "MI (D)" value in both columns "A" and "B" and execute the code only once? Each file is unique so I'm looking for one macro that is all encompassing.

VBA Code:
Sub Add_ON_Lanes()

If Range("A:A").Find(What:="ON", LookAt:=xlPart) Is Nothing Then
    
    ActiveSheet.Range("B:B").Replace What:="MI (D) ", replacement:="MI (D)"
    Range("C:C").NumberFormat = "0.0"
        
    Const SOURCE_FIRST_CELL_ADDRESS As String = "B2"
    Const CRITERION As String = "MI (D)"
    Const DESTINATION_COLUMN As String = "A"
    Const DESTINATION_ROWOFFSET As Long = 4

    Dim ws As Worksheet: Set ws = ActiveSheet
    ' The Find method will fail if the worksheet is filtered:
    If ws.FilterMode Then ws.ShowAllData
    
    Dim srg As Range
    With ws.Range(SOURCE_FIRST_CELL_ADDRESS)
        Set srg = Intersect(.Resize(ws.Rows.Count - .Row + 1), ws.UsedRange)
    End With
    
    Dim slCell As Range: Set slCell = srg.Cells(srg.Cells.Count) ' last
    
    Dim dfCell As Range
    Set dfCell = slCell.Offset(1).EntireRow.Columns(DESTINATION_COLUMN) ' first
    
    Dim sfCell As Range
    ' If the cells contain values:
    Set sfCell = srg.Find(CRITERION, slCell, xlFormulas, xlWhole)
    ' If the cells contain formulas, replace 'xlFormulas' with 'xlValues'.
    ' in the latter case, make sure there are no hidden rows,
    ' or the Find method will fail.
    
    If sfCell Is Nothing Then
        MsgBox "The criterion '" & CRITERION & "' was not found.", vbExclamation
        Exit Sub
    End If
    
    Dim SourceFirstCellAddress As String
    SourceFirstCellAddress = sfCell.Address
    
    Do
        WriteMyData sfCell, dfCell ' write
        Set sfCell = srg.FindNext(sfCell) ' find next
        Set dfCell = dfCell.Offset(DESTINATION_ROWOFFSET)
    Loop Until sfCell.Address = SourceFirstCellAddress
    
Else
    MsgBox "Customer already has Ontario lanes!", vbOKOnly, "Whoops!"
    Exit Sub
End If

End Sub

Sub WriteMyData( _
        ByVal sfCell As Range, _
        ByVal dfCell As Range)
    
    Dim sData() As Variant: sData = sfCell.Offset(, -1).Resize(, 4).Value
    Dim dData() As Variant: ReDim dData(1 To 4, 1 To 4)
    
    dData(1, 1) = sData(1, 1)
    dData(2, 1) = "ON (D)"
    dData(3, 1) = sData(1, 1)
    dData(4, 1) = "ON (I)"
    
    dData(1, 2) = "ON (D)"
    dData(2, 2) = sData(1, 1)
    dData(3, 2) = "ON (I)"
    dData(4, 2) = sData(1, 1)
    
    dData(1, 3) = sData(1, 3) - 1
    dData(2, 3) = sData(1, 3) - 1
    dData(3, 3) = sData(1, 3) - 11
    dData(4, 3) = sData(1, 3) - 11
    
    dData(1, 4) = sData(1, 4) + 25
    dData(2, 4) = sData(1, 4) + 25
    dData(3, 4) = sData(1, 4) + 50
    dData(4, 4) = sData(1, 4) + 50
    
    dfCell.Resize(4, 4).Value = dData


End Sub

I don't need the AL, AR, AZ values returned
Bad lanes.PNG


I need this value returned, as well as the "IN" / "MI (D)" further down
Good lane.PNG


This is what I need the code to do:
Desired end result.PNG
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,224,815
Messages
6,181,135
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