简体   繁体   中英

Using Find functions to paste the found Entire Row

I am trying to use a Find function to find the value of the Active cell in Sheet 2 (I force the active cell to be selected in column A, so if I select "B4", the active cell is "A4"). I would like the Find function to find the value of the "A" active cell from Sheet 2 and find it in Sheet 1, then paste the entire row of the that found row from Sheet 1 into row 2 of Sheet 2.

To break it down:

  • Find the value of Sheet 2 column "A" active cell inside of column A in Sheet 1
  • Copy the entire row into Sheet 2 row 2 of the found column A in sheet 1

I am receiving the following error: Run-time error 1004: "Method 'Range' of object '_Worksheet' Failed https://docs.microsoft.com/en-us/office/vba/api/overview/language-reference

I tried to use a variety of combinations to do this, here is a snapshot of my attempts:

Sheet2.Range("A2").EntireRow.Value = Sheet1.Range("A:A").Find(Sheet2.Range("ActiveCell"), , xlValues, xlWhole).EntireRow.Value
        'Sheet2.Range("A2").EntireRow.Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range("A" & ActiveCell), , xlValues, xlWhole).EntireRow.Value
        
        'Sheet2.Range("A2").Resize(90).Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range(Target.Value), , xlValues, xlWhole).Resize(90).Value
        
        'Sheet2.Rows(2).EntireRow.Value = Sheet1.Range("A:A").Find(Sheet2.Range("ActiveCell"), , xlValues, xlWhole).EntireRow.Value
       
       'Sheet2.Range("A2").EntireRow.Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range(ActiveCell), , xlValues, xlWhole).Resize(90).Value
       'Sheet2.Range("A2").Resize(90).Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range(ActiveCell), , xlValues, xlWhole).Resize(90).Value

Below is the entire code, between where the code is commented Testing below & Testing Above is where my attempts are:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    If Target.Row > 2 Then                                                                      'Doesn't Allow the "Titles" in Row 1 to be highlighted or changed

    Application.EnableEvents = False
        
        'ActiveWorkbook.Names.Add Name:="MyRange", RefersToR1C1:=Range("A" & (ActiveCell.Row))   'Defines the name of the ActiveCell as "MyRange" to Autofill VLookup Formula on sheet
        
        Range("A" & (ActiveCell.Row)).Select                                                    'Always Selects Column A depending on the Active Row selecte

        ActiveSheet.UsedRange.Offset(1).EntireRow.Interior.ColorIndex = 0                       'Clears the previous Active Row's interior colour (yellow)

        Target.EntireRow.Interior.Color = RGB(243, 243, 123)                                    'Sets the current Active Row's interior colour (as yellow)

'Testing below

        Sheet2.Range("A2").EntireRow.Value = Sheet1.Range("A:A").Find(Sheet2.Range("ActiveCell"), , xlValues, xlWhole).EntireRow.Value
        'Sheet2.Range("A2").EntireRow.Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range("A" & ActiveCell), , xlValues, xlWhole).EntireRow.Value
        
        'Sheet2.Range("A2").Resize(90).Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range(Target.Value), , xlValues, xlWhole).Resize(90).Value
        
        'Sheet2.Rows(2).EntireRow.Value = Sheet1.Range("A:A").Find(Sheet2.Range("ActiveCell"), , xlValues, xlWhole).EntireRow.Value
       
       'Sheet2.Range("A2").EntireRow.Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range(ActiveCell), , xlValues, xlWhole).Resize(90).Value
       'Sheet2.Range("A2").Resize(90).Value = Sheet1.Range("A1:A100000").Find(Sheet2.Range(ActiveCell), , xlValues, xlWhole).Resize(90).Value
        
'Testing above
        
        If Target.Address = "$A$2" Then                                                         'Checks if you have selected Row 2 (The comparison row)

            Target.Value = ""                                                                   'If Cell A2 is selected (the "Key" comparison cell from the comparison row) then a blank value is inputted

        Else                                                                                    'If Cell A2 is not selected

            [a2] = ActiveCell                                                                   'Makes cell "A2" equal to the Active Cell value (The "Key" in this case)

        End If                                                                                  'End IF statement

        Me.Range("B2:CK2").Interior.Color = xlNone                                              'Clears any previous (if any) colouring inside cells

        Dim rng As Range                                                                        'Declares variable as a range to store values

        For Each rng In Me.Range("D2:CK2")                                                      'Declares which columns to highlight yellow if there are any parameters in Sheet 2 that vary from Sheet 1

            If IsNumeric(rng.Value) And IsNumeric(Me.Cells(Target.Row, rng.Column)) Then        '[Exludes the Key, Date, Time & Part columns: hence starting at Column D for highlighting variances]

                If rng.Value <> Me.Cells(Target.Row, rng.Column).Value Then                     'Checks if the parameters vary from the main Database ("HE 171")

                    rng.Interior.Color = vbYellow                                               'Highlights any varying parameters in Yellow

                End If                                                                          'End the first IF statement

            End If                                                                              'End the second IF statement

        Next                                                                                    'Compares the next parameter until it reaches the last parameter

    End If                                                                                      'End the initial IF statement at the beginning of the macro

    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub
Sheet2.Range("A2").EntireRow.Value = Sheet1.Range("A:A").Find(Sheet2.Range("ActiveCell"), , xlValues, xlWhole).EntireRow.Value

This is problematic for at least two reasons:

  • Sheet2.Range("ActiveCell") would only work if there were a range named ActiveCell on Sheet2 .
  • If the .Find fails (ie if it doesn't find the value in question), then this will throw an error.

To fix:

  • Change to Sheet2.Range("A" & ActiveCell.Row) .
  • Test if the Find succeeded, before doing anything else, like the following:
Dim foundRange as Range
Set foundRange = Sheet1.Range("A:A").Find(Sheet2.Range("ActiveCell"), , xlValues, xlWhole)

If Not foundRange is Nothing Then
    Sheet2.Range("A2").EntireRow.Value = foundRange.EntireRow.Value
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM