Using FIND and copying each result to a new worksheet

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
881
Office Version
  1. 365
Platform
  1. Windows
This standard FIND and FINDNEXT code block works well and correctly every time in finding any value in any column of any sheet
and gets its values from the userform shown which has Textbox1 for any value of x and Combobox1 for a selection of which Worksheet to select
Code:
  Dim x As String
  Dim y As Variant
  Dim c As Range
  x = Me.TextBox1.value
  y = Me.ComboBox1.value
   'On Error Resume Next
    With Worksheets(y).Range("D1:D500")
      Set c = .FIND(x, LookIn:=xlValues)
        If Not c Is Nothing Then
           firstaddress = c.Address
             Do
              Worksheets(y).Visible = True
              Worksheets(y).Select
              c.Select
              MsgBox "Value found in cell " & c.Address
              Range(Cells(c, 1), Cells(c, 9)).copy  ---------->  (I get a 'type mismatch error here) - Why ? This just says to copy cols 1 to 9 of x.
'              Sheets("REPORT2").Select Sheets("REPORT2").Range("A2")
              Set c = .FindNext(c)
             Loop While Not c Is Nothing And c.Address <> firstaddress
        End If
    End With
End If

Once each value of x is found, I jut want to copy that row from column 1 to column 9 to a new sheet(REPORT2) starting in cell A2 of sheet REPORT2
I'm having trouble with the correct copy code to do this. The images are below.
Please help with this if you can. Thanks cr
 

Attachments

  • USERFORM.JPG
    USERFORM.JPG
    18.5 KB · Views: 9
  • SHEET.JPG
    SHEET.JPG
    42.7 KB · Views: 9

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You might try ...
VBA Code:
Sub chazrab()

    Dim x As String
    Dim y As Variant
    Dim c As Range
    Dim FirstAddress As String
    Dim rw As Long
    
    x = Me.TextBox1.Value
    y = Me.ComboBox1.Value
    'On Error Resume Next
    With Worksheets(y).Range("D1:D500")
        Set c = .Find(x, LookIn:=xlValues)
        If Not c Is Nothing Then
        
            ' Added
            rw = 2                                      '
            
            FirstAddress = c.Address
            Do
                Worksheets(y).Visible = True
                Worksheets(y).Select
                c.Select
                MsgBox "Value found in cell " & c.Address
'                Range(Cells(c, 1), Cells(c, 9)).Copy  ' ---------->  (I get a 'type mismatch error here) - Why ? This just says to copy cols 1 to 9 of x.

                ' changed
                Range(Cells(c.Row, 1), Cells(c.Row, 9)).Copy Destination:=Sheets("REPORT2").Range("A" & rw)
                ' Added
                rw = rw + 1
                
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAddress
        End If
    End With
End Sub
 
Upvote 0
You might try ...
VBA Code:
Sub chazrab()

    Dim x As String
    Dim y As Variant
    Dim c As Range
    Dim FirstAddress As String
    Dim rw As Long
   
    x = Me.TextBox1.Value
    y = Me.ComboBox1.Value
    'On Error Resume Next
    With Worksheets(y).Range("D1:D500")
        Set c = .Find(x, LookIn:=xlValues)
        If Not c Is Nothing Then
       
            ' Added
            rw = 2                                      '
           
            FirstAddress = c.Address
            Do
                Worksheets(y).Visible = True
                Worksheets(y).Select
                c.Select
                MsgBox "Value found in cell " & c.Address
'                Range(Cells(c, 1), Cells(c, 9)).Copy  ' ---------->  (I get a 'type mismatch error here) - Why ? This just says to copy cols 1 to 9 of x.

                ' changed
                Range(Cells(c.Row, 1), Cells(c.Row, 9)).Copy Destination:=Sheets("REPORT2").Range("A" & rw)
                ' Added
                rw = rw + 1
               
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAddress
        End If
    End With
End Sub
Works great!. How simple. Thanks GWteB
 
Upvote 0
You are welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,214,574
Messages
6,120,329
Members
448,956
Latest member
Adamsxl

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