如何在工作簿中使用VBA UserForm循环几个工作表

刚刚更新我的数据,所以你可以看到。 因为我正在引用该工作表,它正在为1工作表。 仍然很难弄清楚如何引用整个工作簿,所以它会在所有31个工作表中查找名称。 如果患者一年中有一次以上的话,我也不知道如何去下一个logging。 任何帮助是非常appriciated。

好的,我的工作簿中的所有工作表都有一个问题循环。 目标我正在努力完成。

  1. 按患者姓名searchlogging。 一旦发现该logging中的所有单元格都将被导入到usrform中。
  2. 能够编辑我需要的任何信息并将其保存回到相同的logging中。

我find了一个关于如何通过工作表完成而不是整个工作簿的Youtubevideo 。 此代码将在现有的用户窗体上进行。 它将需要一个function,它可以让我select下一次病人出现。 所以可能有多个病人的条目。 希望能够select年份和病人作为search标准。

Private Sub CommandButton1_Click() Dim ws As Worksheet Dim found As Range row_number = 0 Do DoEvents row_number = row_number + 1 item_in_review = Sheets("2012").Range("A" & row_number) If item_in_review = Patients_Name.Text Then Date_of_Incident.Text = Sheets("2012").Range("B" & row_number) Month.Text = Sheets("2012").Range("C" & row_number) Year.Text = Sheets("2012").Range("D" & row_number) End If Loop Until item_in_review = "" End Sub 

循环浏览工作簿中的所有工作表

 Option Explicit Dim ws As Worksheet For Each ws in ThisWorkbook.Sheets '' do stuff with ws here Next ws 

还有几件事情可以改善你的代码…

 Sub TestStuff() Dim ws As Worksheet Dim rng As Range Dim found As Range Dim firstAddress As String For Each ws In ThisWorkbook.Sheets '' set the range you want to search in Set rng = ws.Range("D1:D" & ws.Range("D" & ws.Rows.Count).End(xlUp).Row) '' see if it contain's the patient's name (can make this case insensitive) Set found = rng.Find("Patient's Name Here", SearchDirection:=xlNext) '' if it found something If Not found Is Nothing Then firstAddress = found.address '' loop until we hit the first cell again Do '' set textbox values Date_Of_Incident.Text = found.Offset(,-3).Value Month_Of_Incident.Text = found.Offset(,-2).Value Year_Of_Incident.Text = found.Offset(,-1).Value Set found = rng.Find("Patient's Name Here", SearchDirection:=xlNext, After:=found) Loop While Not found Is Nothing And found.address <> firstAddress End If Next ws End Sub 

请注意,如果电子表格中有一个病人有多个条目,则会查找所有条目,但只显示最后一个条目的信息。

如果你想要我的两分钱,我会使用列表框来列出病人所有约会的date,这样你就可以看到“哦,这个用户有4个约会,这是我想看的。 “ 然后你点击你想要的ListBox项,并且它有一些存储在其中的信息,说这个ListBox项对应于工作表中的这个项目。 然后从工作表中拉取该信息并填充UserForm。 再次,只是我的两分钱根据我读过的。


在聊天讨论后,最终的代码是这样的:

 Option Explicit Private Sub AddWithValue(Text As String, Value As String) lbxAppointments.AddItem Text lbxAppointments.List(lbxAppointments.ListCount - 1, 1) = Value End Sub Private Sub btnSearch_Click() Dim ws As Worksheet Dim search As Range Dim found As Range Dim patient As String Dim lbxValue As String Dim firstFind As String lbxAppointments.Clear patient = tbxPatientName.Text For Each ws In ThisWorkbook.Sheets '' define our search range (Column A) Set search = ws.Range("A1:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row) '' search that range for the patient's name Set found = search.Find(patient, SearchDirection:=xlNext) '' test if we found anything If Not found Is Nothing Then firstFind = found.Address Do '' found something, add it to the text box lbxValue = "'" & found.Parent.Name & "'!" & found.Address(External:=False) AddWithValue found.Offset(, 1).Value, lbxValue Set found = search.Find(patient, SearchDirection:=xlNext, After:=found) Loop While Not found Is Nothing And found.Address <> firstFind End If Next ws End Sub Private Sub lbxAppointments_Change() Dim rng As Range With lbxAppointments If .ListIndex <> -1 Then Set rng = Range(.List(.ListIndex, 1)) '' now get all of the offsets of it here and you can populate textbox controls with the info '' rng.Offset(,1) = Column B '' rng.Offset(,2) = Column C '' rng.Offset(,3) = Column D, so on and so forth End If End With End Sub