Macro to look up Request Number and copy to new cell

razor22

New Member
Joined
Jul 28, 2011
Messages
6
I am trying to use a macro that will look up "RequestNumber" from Col A, copy all information on that Row (B:F), and paste it into a new sheet. The code used to use this same exact concept with Date (instead of RequestNumber). :
Sub CopyDateRows()
Dim myDate As String
Dim lr As Long

myDate = InputBox("Enter Date")
If IsDate(myDate) Then
Application.ScreenUpdating = False
ActiveSheet.Copy After:=ActiveSheet
With ActiveSheet
lr = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:A" & lr)
.AutoFilter Field:=1, Criteria1:= _
"<>" & CLng(DateValue(myDate))
.Offset(1).EntireRow.Delete
.AutoFilter
End With
End With
Application.ScreenUpdating = True
Else
MsgBox "No valid date entered"
End If
End Sub


Here is what I have so far to use on my book. I am a beginner so please bear with me. Any help is greatly appreciated. I will be online for quick replys.

Sub CopyNumberRows()
Dim myRequestNumber As String
Dim lr As Long

myRequestNumber = InputBox("Enter Request Number")
If IsRequestNumber(myRequestNumber) Then
Application.ScreenUpdating = False
ActiveSheet.Copy After:=ActiveSheet
With ActiveSheet
lr = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:A" & lr)
.AutoFilter Field:=1, Criteria1:= _
"<>" & CLng(DateValue(myRequestNumber))
.Offset(1).EntireRow.Delete
.AutoFilter
End With
End With
Application.ScreenUpdating = True
Else
MsgBox "No valid request number entered"
End If
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
We'll assume that IsRequestNumber() is a function that returns true/false depending whether the value is found or not.


Code:
Sub CopyNumberRows()
Dim myRequestNumber As String
Dim lr As Long
 
myRequestNumber = InputBox("Enter Request Number")
If IsRequestNumber(myRequestNumber) Then
Application.ScreenUpdating = False
ActiveSheet.Copy After:=ActiveSheet
With ActiveSheet
lr = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:[COLOR=blue][B]A[/B][/COLOR]" & lr) ' [COLOR=blue]May need to extend this column label [/COLOR]
'.AutoFilter Field:=1, Criteria1:= _
'"<>" & CLng(DateValue(myRequestNumber))
 
[COLOR=blue]'Make sure this indicates the column that contains the RequestNumber within the data list[/COLOR]
.AutoFilter Field:=[COLOR=blue]1[/COLOR], Criteria1:= _
"<>" & myRequestNumber [COLOR=blue]'Stripped off date conversion functions[/COLOR]
 
.Offset(1).EntireRow.Delete
.AutoFilter
End With
End With
Application.ScreenUpdating = True
Else
MsgBox "No valid request number entered"
End If
End Sub
 
Upvote 0
Thank you for your response Tweedle.

That is exactly what I want to do. Return True/False if the value is found or not and then copy it to new sheet.

When I am running the macro I am getting an error: Can't Execute in Break Mode and then it is going back to the code. Can you please help me with this?

Thank you very much for you time and help
 
Upvote 0
I'm not precisely familiar with that error.
The below example works for the functionality you're looking for.

Code:
Sub CopyNumberRows()
    Dim myRequestNumber As String
    Dim lr As Long
    myRequestNumber = InputBox("Enter Request Number")
    If IsRequestNumber(myRequestNumber) Then
        Application.ScreenUpdating = False
        ActiveSheet.Copy After:=ActiveSheet
        With ActiveSheet
            lr = .Range("A" & .Rows.Count).End(xlUp).Row
            With .Range("A1:[COLOR=blue]H[/COLOR]" & lr)
                [COLOR=blue]'Make sure this indicates the column that contains the RequestNumber within the data list[/COLOR]
[COLOR=blue]               'REQ NUM (2) in example[/COLOR]
                .AutoFilter Field:=[COLOR=blue]2[/COLOR], Criteria1:="<>" & myRequestNumber
                .Offset(1).EntireRow.Delete
                .AutoFilter
            End With
        End With
        Application.ScreenUpdating = True
    Else
        MsgBox "No valid request number entered"
    End If
End Sub


Excel Workbook
ABCDEFGH
1DATEREQ NUMDATA 1DATA 2DATA 3DATA 4DATA 5DATA 6
28/3/201112346810121416
38/3/2011129091215182124
48/3/20111346121620242832
58/3/20111402152025303540
68/3/20111458182430364248
78/3/20111514212835424956
88/4/20111570243240485664
98/4/20111514273645546372
108/4/20111458304050607080
118/4/20111402334455667788
128/4/20111346364860728496
138/4/201112903952657891104
148/4/201112344256708498112
158/4/2011117845607590105120
168/4/2011112248648096112128
Before Code
Excel Workbook
ABCDEFGH
1DATEREQ NUMDATA 1DATA 2DATA 3DATA 4DATA 5DATA 6
28/3/2011129091215182124
38/4/201112903952657891104
4
After Code


This is my version of the IsRequestNumber function:
Code:
Function IsRequestNumber(ReqNum)
    Dim rng As Range
    IsRequestNumber = False
 
    Set rng = Cells.Find(What:=ReqNum, After:=Range("A1"), LookIn:=xlValues, LookAt _
    :=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlAll, MatchCase:= _
    False, SearchFormat:=False)
 
    If Not rng Is Nothing Then
        IsRequestNumber = True
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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