简体   繁体   中英

find text in column in one sheet and copy row data to another sheet

I have a spreadsheet with customer information that I want to search on by last name. I want to enter the last name on a separate sheet (Sheet 1) and have the macro search the Last Name column in the customer data spreadsheet (Sheet 2). When it finds a match, I want it to copy the entire row in Sheet 2 and paste it to a specific row in Sheet 1. I've searched a number of sites and tried numerous versions of code but cannot get it to work.

Here's a link that shows you how to get data from another sheet or workbook. Basically you use Sheet_name!Cell_address or Sheet_name!First_cell:Last_cell .

Hope this helps :)

I think this sounds simple enough, loop until you find the value you want. How do you want the trigger to fire? the below into sheet 2 into the will trigger after double clicking on a selected cell in column 1, will prompt you for input, then copy the first match.

Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 1 Then Exit Sub 'or which ever column you enter for
Dim str_Act, str_Test As String
Dim i As Integer
'find value to search
str_Act = InputBox("Enter User Last Name")
If str_Act = "" Then Exit Sub
'loop to find search
Do While str_Act <> str_Test
    str_Test = Sheets(1).Range("A1").Offset(i, 0) ' or whichever column has your value
    i = i + 1
Loop
'Copy and paste
Sheets(1).Range("A1:ZZ1").Offset(i - 1, 0).Copy
Target.PasteSpecial
End Sub

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