如何使用VBA用户窗体循环工作簿中的多个工作表

时间:2014-06-09 15:56:37

标签: excel vba excel-vba userform

刚刚更新了我的数据,您可以看到。因为我正在引用该工作表,所以它为1个工作表而烦恼。仍然很难弄清楚如何引用整个工作簿,所以它将在所有31个工作表中查找名称。如果患者每年在那里不止一次,我也很难弄清楚如何进入下一个记录。任何帮助都很受欢迎。

好的,我在工作簿中的所有工作表中遇到问题。我想要实现的目标。

  1. 按患者姓名搜索记录。一旦找到,该记录中的所有单元格都将导入到usrform中。
  2. 能够编辑我需要的任何信息,并将其保存回同一记录中。
  3. 我找到了Youtube video如何通过工作表而不是整个工作簿来完成它。此代码也将在现有的用户表单上进行。它需要一个功能,它允许我在患者下次出现时选择。因此,该患者可能有多个条目。希望能够选择年份和患者作为搜索标准。

          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
    

1 个答案:

答案 0 :(得分:5)

循环遍历工作簿中的所有工作表

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

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

如果你想要我的两分钱,我会使用一个ListBox来列出患者所有约会的日期,这样你就可以看到&#34;哦,这个用户有4个约会,这就是一个我想看看。&#34;然后单击所需的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