Finding Cell in another sheet

2019-07-29 03:49发布

I created a database for students who have applied for payment plan. I have 3 sheets:

"Enquiry" - this is where the data is stored while waiting for the students to sign up.

"Confirmed" - this data has 3 row headings: Student ID (B1), Plan ID (B2) and Pay ID (B3).This is where I will input the additional data after the student signs up.

"Masterlist" - the final list of students who confirmed the payment plan.

The idea is for me to manually input the data in the "Confirmed" sheet then run a macro that searches the value of the Student ID (B1) in the "Enquiry" sheet. I then want it to copy the Plan ID (B2) and Pay ID (B3) in column B and E on the "Enquiry" sheet.

Once the data is populated, I need the whole row transferred to the "Masterlist" sheet.

I've tried recording Marco but what it finds it only the current value of the cell when I recorded it. I'm fairly good with formula but haven't really done macros before.

Range("B1").Select
Selection.Copy
Sheets("Enquiry").Select
Cells.Find(What:="4536092", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
Rows("29:29").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Masterlist").Select
ActiveWindow.LargeScroll Down:=-7
Rows("2:2").Select
Sheets("Enquiry").Select
Range("A29").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "4536092"
Rows("29:29").Select
Selection.Copy
Sheets("Masterlist").Select
Rows("2:2").Select
Selection.Insert Shift:=xlDown

1条回答
够拽才男人
2楼-- · 2019-07-29 04:02

Not all of vba goodness is accessible via the macro recorder. Code below works in the test workbook I built based your sheet layout description. In the future, please add screenshots or better yet a dropbox xlsx of your workbook. Also note that very few people here will take the time to build your code from scratch.

I've commented this out quite a bit. Play with this. Comment with questions and I will answer.

Sub macroName()

        'declaring range object Ranges hold values and cell addresses at the same time
    Dim id As Range
    Dim found As Range

        'setting id var
    Set id = Worksheets("Confirmed").Range("b2")

        'making sure id we are checking isn't null
    If Not id.Value = "" Then
            'finding the value for var id.  xlWhole means exact cell match.
        Set found = Worksheets("Enquiry").Cells.find(What:=id, LookAt:=xlWhole)
        MsgBox "Inserted new row in Masterlist for ID: " & id
    Else
        MsgBox "ID cannot be null at: " & id.Address
    End If

        'making sure we had a match from .find
    If Not found Is Nothing Then
            'inserting a new row
        Worksheets("Masterlist").Range("a2").EntireRow.Insert
            'setting new row to value of entire row value of range object.
        Worksheets("Masterlist").Range("a2").Value = found.Rows.Value
    Else
        MsgBox "Nothing found for id: " & id.Value
    End If
End Sub
查看更多
登录 后发表回答