+ Reply to Thread
Results 1 to 2 of 2

Search, copy & paste Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    11-01-2012
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    1

    Search, copy & paste Macro

    Hello,
    First of all, i will thank all who reads this question.

    And such a good forum.
    I have a question regarding a Macro.

    Basically i have spreadsheet which is a small database.


    Sheet1 is a database. Sheet1 keeps all data
    Sheet2 is a inputsheet or form.

    What i need is, that when input is done, than it will search in Database and match it, copy the values to database.


    Sheet2:
    C E F
    Name Input Age Input weight
    16 John

    Ex. i am going to fill age and weight in E16 and F16.


    So what i want a macro in input sheet. And macro has to copy from E16 & F16, than search for "John" in sheet1 and transfer data to sheet1 when it can mach "John" in sheet1. All names are placed in Column H in sheet1.

    John is placed in Column H with names on Sheet1 on row 23. So the data from input has to be copied to sheet1 at C23&G23.


    And it will be delightfull with a standard error message, when or if John is not listed in database or sheet1. And maybe with a msg that "copy succesful" if a match.

    I hope you can help me. I will be happy. Atttaching file too....
    Attached Files Attached Files

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Search, copy & paste Macro

    This should do it

    Sub abc()
     Const shArk1 As String = "Ark1"
     Const shArk2 As String = "Ark2"
     Dim FoundCell As Range
     Dim sName As String, sAge As String, sWeight As String
     
     With Worksheets(shArk2)
        sName = .Range("c16").Value
        sAge = .Range("e16").Value
        sWeight = .Range("f16").Value
     End With
     If Not sName <> "" Then Exit Sub
     With Worksheets(shArk1)
         Set FoundCell = .Range("h:h").Find(What:=sName, LookAt:=xlWhole)
         If Not FoundCell Is Nothing Then
            FoundCell.Offset(, -5) = sAge
            FoundCell.Offset(, -1) = sWeight
         Else
            MsgBox "Name not found."
           Exit Sub
         End If
     End With
     MsgBox "Update succesful."
     
     With Worksheets(shArk2)
        .Range("c16").ClearContents
        .Range("e16").ClearContents
        .Range("f16").ClearContents
     End With
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1