Excel VBA – 在列中查找string并在该行中插入数据

我想弄清楚如何使用Excel来扫描一个特定的数字表,例如数字50为例。 当我的代码在表格中find数字50时,我想要设置其中的50行,并在其右侧插入一些用户定义的文本。 然后我有各种各样的VLOOKUP,MATCH和IFERRORfunction已经build立,将从那里工作。 但是现在我有点麻烦了。 我正在使用用户窗体插入数据。 上下文是一个学生注册系统,学生证将会出现,当用户select一个课程ID将自动分配给该学生。

我使用了类似的代码来将数据input分配到下一空闲行。 但是我想要把这个数据项分配给一个特定的行,而不是下一个空闲的行。 我以前使用的代码如下。

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(Last Row)循环closures表单。 我发现工作得很好。

任何帮助将不胜感激!

编辑

这是完成的代码所有工作,感谢保罗!

 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 

我发现很难理解你在找什么。 如果要查找包含值的行,可以使用查找function

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