简体   繁体   中英

Excel VBA - Find string in column and insert data in that row

I'm trying to figure out how to use excel to scan a table for a specific number, say the number 50 for the sake of the example. When my code finds the number 50 in the table I want to set that row where 50 is and insert some user defined text in to the right of it. I then have all sorts of VLOOKUPs, MATCH and IFERROR functions already set up that will work on from there. But I'm having a bit of trouble at the moment. I'm using a Userform to insert the data. The context is a Student Enrolment system, the student ID will be present and when the user selects a course that course ID will automatically be assigned to said student.

I've used similar code to assign data entry to the next free row. But I want this to assign the data entry to a specific row, not just the next free one. The code I used previously is as follows.

Dim LR As Integer, Master As Worksheet Set Master =
ThisWorkbook.Worksheets("Student")

LR = 6

If LR = 6 And Cells(LR, "A").Value = "" Then 
    LR = LR 
Else 
    LR = LR + 1
    If Cells(LR, "A").Value = "" Then 
    Else 
        Do Until Cells(LR, "A").Value = "" 
            LR = LR + 1 
        Loop 
    End If 
End If

Master.Cells(LR, "A").Value = (Me.txtID) 
Master.Cells(LR, "B").Value =(Me.txtFore) 
Master.Cells(LR, "C").Value = (Me.txtSur)
Master.Cells(LR, "D").Value = (Me.txtAddress) 
Master.Cells(LR, "E").Value = (Me.cmbSex)

Unload UserForm1

As you can see, that code sucessfully assigns the data entry to the next available row and then closes the form using the LR (Last Row) loop. I found that worked quite well.

Any help would be greatly appreciated!

EDIT

Here's the finished code all working, thanks to Paul!

Dim FR As Integer, Course As Worksheet, CourseCode, StudentID As String
Set Course = ThisWorkbook.Worksheets("Course")
Set Enrol = ThisWorkbook.Worksheets("Enrolment")
StudentID = txtID
CourseCode = cmbCourse
FR = 6
Dim lRow As Long



If FR = 6 And Course.Cells(FR, "B").Value = CourseCode Then
FR = FR
Else
    FR = FR + 1
    If Course.Cells(FR, "B").Value = CourseCode Then
    Else
        Do Until Course.Cells(FR, "B").Value = CourseCode
        FR = FR + 1
        Loop
    End If
End If

lRow = Enrol.Range("A6:A45").Find(StudentID).Row

Select Case CourseCode
Case "Animal Care"
CourseCode = "9841"
Case "Animation"
CourseCode = "3320"
Case "Art"
CourseCode = "6387"
Case "Biology"
CourseCode = "4685"
Case "Business Studies"
CourseCode = "5879"
Case "Calculus"
CourseCode = "4123"
Case "Chemistry"
CourseCode = "1586"
Case "Computing"
CourseCode = "3669"
Case "Dance"
CourseCode = "4521"
Case "Design and Tech"
CourseCode = "5478"
Case "Drama"
CourseCode = "5678"
Case "Engineering"
CourseCode = "6321"
Case "English Language"
CourseCode = "4768"
Case "English Literature"
CourseCode = "3908"
Case "Fashion Design"
CourseCode = "2477"
Case "Film Making"
CourseCode = "4489"
Case "French"
CourseCode = "2548"
Case "Functional Skills"
CourseCode = "2685"
Case "Geography"
CourseCode = "8874"
Case "German"
CourseCode = "9512"
Case "Graphic Design"
CourseCode = "5232"
Case "History"
CourseCode = "4895"
Case "Italian"
CourseCode = "6578"
Case "Japenese"
CourseCode = "5988"
Case "Korean"
CourseCode = "9874"
Case "Latin"
CourseCode = "3478"
Case "Law"
CourseCode = "2321"
Case "Mathmatics"
CourseCode = "9458"
Case "Media Studies"
CourseCode = "1589"
Case "Modern Languages"
CourseCode = "5612"
Case "Nursing"
CourseCode = "2003"
Case "Photography"
CourseCode = "2001"
Case "Physical Education"
CourseCode = "8496"
Case "Physics"
CourseCode = "8534"
Case "Religious Studies"
CourseCode = "2320"
Case "Social Studies"
CourseCode = "2301"
Case "Spanish"
CourseCode = "6217"
Case "Statistics"
CourseCode = "4895"
Case "Textiles"
CourseCode = "2240"
Case "Travel and Tourism"
CourseCode = "5698"
End Select

Enrol.Cells(lRow, "E").Value = (CourseCode)

Unload UserForm3

I'm finding it difficult to understand what exactly you are looking for. If you want to find the row that contains a value you can use the Find function

Dim lRow As Long
lRow = Course.Range("A1:A1000").Find(StudentID).Row

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