VBA Code : Find value and select his row and copy paste it

Silecya

New Member
Joined
May 5, 2020
Messages
5
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello everyone,

After researchs I'm stuck on a VBA code and I'm looking for your help.

You will find the process that I want to do :
1. Excel make a research based on a value that is located in a cell and found it (That value is set in a previous userform).
2. Then, he select the row that match to the cell where value is located
3. Once, he select it, he copy past it in an another sheet.

Step 1 is OK and the research works . I have suggestions on the step 2 but it doesn't work. (See VBA code)

Thanks in advance for your help. If you need further explaination don't hesistate to notice me. =)

Silecya =)

VBA Code:
Sub Selection_recherche_dossier()

'Declare variables
    Dim ws As Worksheet
    Dim SelectCells As Range
    Dim SelectresearchRow As Variant
    Dim xcell As Object
    Dim Rng As Range
    Set ws = Worksheets("Suivi")
    Set Rng = ws.Range("B16:B36")

'check each cell in a specific range if the criteria is matching
    For Each xcell In Rng
    If xcell.Value = Range("E6") Then
    If SelectCells Is Nothing Then
    Set SelectCells = Range(xcell.Address)
        Else
    Set SelectCells = Union(SelectCells, Range(xcell.Address))
    End If
    End If
 
    Next
'select the cells with specified value
    'SelectCells.Select

[B]'Everthing works until that point

'Select Row and Copy to another worksheet : Suggestion[/B]
    Dim sourceRange As Range
    Dim destrange As Range
    Dim Lr As Long
    Lr = LastRow(Sheets("Affichage Recherche")) + 1
    Set sourceRange = Sheets("Suivi").Cells( _
    ActiveCell.Row, 1).Range("B16:AH36")
    With sourceRange
        Set destrange = Sheets("Affichage Recherche").Range("B" _
        & Lr).Resize(.Rows.Count, .Columns.Count)
    End With
    destrange.Value = sourceRange.Value
    
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this

VBA Code:
Sub Selection_recherche_dossier()
  Application.ScreenUpdating = False
  With Sheets("Suivi")
    .Range("B15:AH36").AutoFilter 1, .[E6]
    .AutoFilter.Range.Offset(1).Copy
    Sheets("Affichage Recherche").Range("B" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
    .ShowAllData
  End With
  Application.ScreenUpdating = True
End Sub

I have the doubt, in which sheet is the value to be compared of the cell: Range("E6").
If it is on sheet "Suivi", the above code is correct, but if it is on sheet "Affichage Recherche" then change to this:

.Range("B15:AH36").AutoFilter 1, Sheets("Affichage Recherche").[E6]
 
Upvote 0
Hello,

Thanks a lot for your quick answer. That works perfectly for me.

The sheet that you have selected in your 1rst VBA code was the good one (y)

Regards :)
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

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