Excel VBA - 在列中查找字符串并在该行中插入数据

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

我正在尝试弄清楚如何使用 excel 来扫描 table 以获取特定数字,为了示例起见,假设数字 50。当我的代码在 table 中找到数字 50 时,我想将该行设置为 50,并在其右侧插入一些用户定义的文本。然后,我已经设置了各种 VLOOKUP、MATCH 和 IFERROR 函数,可以从那里开始工作。但我现在有点麻烦。我正在使用用户表单插入数据。上下文是学生注册系统,学生 ID 将出现,当用户选择课程时,课程 ID 将自动分配给该学生。

我使用类似的代码将数据条目分配给下一个空闲行。但我希望将数据条目分配给特定的行,而不仅仅是下一个空闲行。我之前使用的代码如下。

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

如您所见,该代码成功地将数据条目分配给下一个可用行,然后使用 LR(最后一行)循环关闭表单。我发现效果很好。

如有任何帮助,我们将不胜感激!

编辑

这是完成的所有代码,感谢 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

我发现很难理解您到底在寻找什么。如果要查找包含值的行,可以使用 Find 函数

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