我得到床单,第一张包含身份证和医院名称,第二张有身份证(医院依赖)和病房名称。
通过数据验证列表,我已经获得了主要列表,现在我正在寻找如何过滤二级选择列表的方式,该列表仅显示具有相同ID的病房,如医院。 在此先感谢!!
答案 0 :(得分:0)
也许你可以用它来处理它。我设计了一个简单的Excel。我的表1被称为“医院名单”,它包含医院的ID和名称。
我的表2,名为“WARDENS LARD”,是所有医院的所有看守人员名单,与当时的医院ID有关。
此外,我只创建了一个名为“My_Wardens”的命名范围,因为我们需要一个dinamic范围作为Wardens数据验证列表的来源。你说创建名称不是一个选项,但我们只创建一个名称,它将始终使用相同的名称,使用VBA代码我们将更改名称的源数据。这就是诀窍。
现在,单元格E11中的Sheet 1是我的第一个数据验证列表。它只显示医院ID。单元格E12是重要的单元格,因为它具有数据验证列表,其中包含与所选医院ID相关的看守。
我的代码就是这个。仅当您更改Cell E11的值时才会触发。根据您的需要调整代码。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False, xlA1) <> "E11" Then Exit Sub 'E11 must be replaced with cell reference where your wardens data validation list appears
Dim MyWardens() As String
Dim Wardens As String
Dim i As Long
Dim HospitalCell As Range
Set HospitalCell = ThisWorkbook.Sheets("LIST OF HOSPITALS").Range(Target.Address) 'Cell Reference where your hospital validation list appears.
i = 2
Wardens = ""
With ThisWorkbook.Sheets("LIST OF WARDENS") 'Sheet name of where the list of wardens is. In my example, data starts at row 1 and Column A is Hospital ID
'and columnb B is Warden name
Do Until .Cells(i, 1).Value = ""
If HospitalCell.Value = .Cells(i, 1).Value Then 'We save just the row reference, because we know the column of Wardens.
If Len(Wardens) = 0 Then
Wardens = i
Else
Wardens = Wardens & "||" & i
End If
End If
i = i + 1
Loop
End With
MyWardens = Split(Wardens, "||")
'We just need first row (it's in Lbound) and last row (It's in Ubound) so we use R1C1 notation, because we know column of Wardens
Wardens = "='LIST OF WARDENS'!R" & MyWardens(0) & "C2:R" & MyWardens(UBound(MyWardens)) & "C2"
ThisWorkbook.Names("My_wardens").RefersToR1C1 = Wardens
End Sub
现在,如果我选择,例如,医院2,Cell E12将显示与该医院相关的Wardens。
如果我选择医院5,也会发生同样的情况。
根据您的需求调整代码。希望这有帮助!
答案 1 :(得分:0)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False, xlA1) <> "U2" Then Exit Sub 'U2 must be
replaced with cell reference where your wardens data validation list appears
'This cell (U2) is on sheet "Form" not sure how to indicate it
Dim MyWardens() As String
Dim Wards As String
Dim i As Long
Dim HospitalCell As Range
Set HospitalCell = ThisWorkbook.Sheets("Hospitals").Range(Target.Address)
'Cell Reference where your hospital validation list appears.
i = 2
Wards = ""
With ThisWorkbook.Sheets("Wards") 'Sheet name of where the list of wards is.
In my example, data starts at row 1 and Column A is Hospital ID
'and columnb B is Ward name
Do Until .Cells(i, 1).Value = ""
If HospitalCell.Value = .Cells(i, 1).Value Then 'We save just the row reference, because we know the column of Wardens.
If Len(Wards) = 0 Then
Wards = i
Else
Wards = Wards & "||" & i
End If
End If
i = i + 1
Loop
End With
MyWardens = Split(Wards, "||")
'We just need first row (it's in Lbound) and last row (It's in Ubound) so we
use R1C1 notation, because we know column of Wardens
Wards = "='Wards'!R" & MyWardens(0) & "N3:R" & MyWardens(UBound(MyWardens)) & "N3"
ThisWorkbook.Names("My_wards").RefersToR1C1 = Wards
End sub
答案 2 :(得分:0)
答案 3 :(得分:0)
答案 4 :(得分:0)
答案 5 :(得分:0)