简体   繁体   中英

How to find and update a row in an Excel sheet based on a value in a column in another sheet?

Looking some guidance. I have two excel sheets (1) masterdata (2) Searchterms.

Searchterms contains a single column A with StudentIDs.
Masterdata contains all data relating to students.

I've never used VBA before in excel so looking some guidance please

What i need to be able to do is take the first studentID from StudentIDs sheet (A1) and search for it on the Masterdata sheet. If found then update the columns in that sheet (masterdata) with several pieces of information... If not found then move onto the next StudentID in col A2 etc on the StudentIds sheet.

Sub FindMatchingValue() 
Dim i As Integer
  Dim strValueToFind As String

    strValueToFind = Worksheets("StudentID").Range("A1").Value
    For i = 1 To 500
        If Cells(i, 1).Value = strValueToFind Then
            MsgBox ("Found value on row " & i)
            'update row i col B  = 'new name' - found in StudentID sheet col B
            'update row i col C  = 'new class number '- found in StudentID sheet col C
            Exit Sub
        End If
    Next i

    MsgBox ("Value not found in the range!")
End Sub

workbook contains two worksheets namely masterdata worksheet with columns....

  • Col A StudentID
  • Col B Name
  • Col C Class Number
  • Col D Enrol Date
  • Col E Start Date
  • Col F Student Loan
  • Col G Comments
  • COL H Last edited

    normally about 5000 rows in this sheet

and

searchterms worksheet with columns.....

  • Col A StudentID
  • Col B Name
  • Col C Class Number
  • Col D Comment

    normally about 1-20 rows in this sheet

Here's something you can try. We have 2 major jobs here.

  1. Find the match. Of course, you can use For Loop as in your attempt but, let's take advantage of Excel's built in method, Find . Find method returns a Range object, meaning it will return the range in masterdata sheet that contains the student ID .

     Dim sID As Range, whattofind As Range Dim mSh As Worksheet, sSh As Worksheet Set mSh = Worksheets("masterdata") '// you can adjust to suit // Set sSh = Worksheets("searchitems") Set whattofind = sSh.Range("A1") With mSh '// here you use the Find Method of the Range Object // Set sID = .Range("A:A").Find(What:=whattofind.Value2, _ After:=.Range("A" & .Rows.Count)) '// take note that we supplied the After argument // '// that is to cover the entire range from top to bottom // End With 
  2. Update the records. Since we already found Range that contains the student ID , we just have to update some columns. To do that, we'll use Excel's built in method, Offset . And take note that we need to do it several times since we have multiple entries in searchitems sheet. So we will use a For Loop too.

     Dim sID As Range, whattofind As Range, i As Long Dim mSh As Worksheet, sSh As Worksheet Set mSh = Worksheets("masterdata") '// you can adjust to suit // Set sSh = Worksheets("searchitems") For i = 1 To 500 '// depends on your list // Set whattofind = sSh.Range("A" & i) With mSh Set sID = .Range("A:A").Find(What:=whattofind.Value2, _ After:=.Range("A" & .Rows.Count)) If Not sID Is Nothing Then '// check if a match is found // '// just play around and adjust on how many columns you need updated // sID.Offset(, 2) = whattofind.Offset(, 2) '// here we update Class number // sID.Offset(, 6) = whattofind.Offset(, 3) '// here we update Comments // End If End With '// reset your ranges // Set sID = Nothing Set whattofind = Nothing Next 

I hope this helps to get you started.

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