Consulting

Results 1 to 5 of 5

Thread: Solved: Using find and copy, but only want it to search in 1st column

  1. #1

    Solved: Using find and copy, but only want it to search in 1st column

    Hello all,

    I'm using the macro below to search for a user-defined number (via message box) in column A, and copy/paste only the rows that begin with that value into another sheet. The only problem is that it currently is looking in all of the columns for the user-defined number and copy/pasting the rows containing it, no matter which column the number appears in. How do I tell it to only grab the rows that have the user-defined number in column A?

    [vba]Sub CopyPaste042013()
    Dim Number As Integer
    Number = Application.InputBox("Enter the number you are looking for below", "Enter your number", , , , , , 1)
    Sheet2.Cells.Delete
    Application.ScreenUpdating = False
    Dim xRow&, NextRow&, LastRow& NextRow = 2
    LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
    For xRow = 1 To LastRow
    If Application.CountIf(Rows(xRow), Number) > 0 Then
    Rows(xRow).Copy Sheets("Sheet3").Rows(NextRow)
    NextRow = NextRow + 1
    End If
    Next xRow
    Application.ScreenUpdating = True
    MsgBox "Macro is complete, " & NextRow - 2 & " rows containing" & vbCrLf & _
    "''" & Number & "''" & " were copied to Sheet2.", 64, "Done"
    End Sub [/vba]
    Thanks so much,
    Nikolette

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    instead of cells.find use columns("A").find

  3. #3
    Will try that, Patel - thank you...

  4. #4
    Nope; still copying every row that has a 1 in any of its columns...

  5. #5
    Turns out replacing what's there with this line works :-):

    [VBA]If Application.CountIf(Range("A" & xRow), Number) > 0 Then[/VBA]

    Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •