Code Adjustment for copying lookup results needed

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I am looking for a fix to this code so that it will look for the InputBox entry from column C and E instead of looking for them from just column C.

Thanks in advance

Code:
Sub AnotherLoop_1063452()
Dim r As Range, rng As Range, rng1 As Range, b As Boolean
Dim arr As Variant
Application.ScreenUpdating = False

Set rng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
b = False
arr = Split(Application.InputBox("Please enter two 2-digit numbers separated by a comma."), ",")
If UBound(arr) <> 1 Then
    MsgBox "Your entry is invalid. Please try again."
    Exit Sub
End If

For Each r In rng
    If InStr(r.Value, arr(0)) > 0 And InStr(r.Value, arr(1)) > 0 Then
        b = True
        On Error GoTo nxt
        Set rng1 = Application.Union(rng1, r)
    End If
Next r
If b = True Then
    rng1.EntireRow.Copy
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Paste
Else
    MsgBox "No match found."
End If

Exit Sub
nxt:
    Set rng1 = r
    On Error GoTo 0
    Resume Next
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
That is one string from column C and another from column E.
Sorry I forgot to clarify that
 
Upvote 0
Try
Code:
Sub AnotherLoop_1063452()
Dim r As Range, rng As Range, rng1 As Range, b As Boolean
Dim arr As Variant
Application.ScreenUpdating = False

Set rng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
arr = Split(Application.InputBox("Please enter two 2-digit numbers separated by a comma."), ",")
If UBound(arr) <> 1 Then
    MsgBox "Your entry is invalid. Please try again."
    Exit Sub
End If

For Each r In rng
    If InStr(r.Value, arr(0)) > 0 And InStr(r.Offset(, 2).Value, arr(1)) > 0 Then
        If rng1 Is Nothing Then Set rng1 = r Else Set rng1 = Application.Union(rng1, r)
    End If
Next r
If Not rng1 Is Nothing Then
    rng1.EntireRow.Copy
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Paste
Else
    MsgBox "No match found."
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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