例如我Sheet1表中有个表是这样的:
班级 人员
1班 张三
1班 李四
1班 王五
2班 陈陈
2班 丹丹
2班 小小
2班 点点
3班 赵大
3班 钱钱
3班 猪猪
3班 笨笨
那么我在Sheet2表中做个表有班级和人员列用到Sheet1表的序列时,假设在A2单元格选1班的时候,B2单元格只能选1班对应的人员,选2班的时候,B2单元格只能选2班对应的人员,如何用VBA代码实现?
在Sheet2都要用下拉菜单实现。
EXCEL2013利用VBA代码按条件引用下拉菜单
答案:1 悬赏:0 手机版
解决时间 2021-02-28 11:08
- 提问者网友:浪荡绅士
- 2021-02-27 13:17
最佳答案
- 五星知识达人网友:妄饮晩冬酒
- 2021-02-27 14:22
sheet2中加入以下代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 2 And Cells(Target.Row, 1) <> "" Then
Sheet1.Range("C:C").Delete
j = 1
For i = 1 To Sheet1.UsedRange.Rows.Count
If Sheet1.Cells(i, 1) = Cells(Target.Row, 1) Then
Sheet1.Cells(j, 3) = Sheet1.Cells(i, 2)
j = j + 1
End If
Next i
Dim cnum
cnum = Application.WorksheetFunction.CountA(Sheet1.Range("c:c"))
If cnum >= 1 Then
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Sheet1!C1:C" & Application.WorksheetFunction.CountA(Sheet1.Range("c:c"))
End With
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 2 And Cells(Target.Row, 1) <> "" Then
Sheet1.Range("C:C").Delete
j = 1
For i = 1 To Sheet1.UsedRange.Rows.Count
If Sheet1.Cells(i, 1) = Cells(Target.Row, 1) Then
Sheet1.Cells(j, 3) = Sheet1.Cells(i, 2)
j = j + 1
End If
Next i
Dim cnum
cnum = Application.WorksheetFunction.CountA(Sheet1.Range("c:c"))
If cnum >= 1 Then
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Sheet1!C1:C" & Application.WorksheetFunction.CountA(Sheet1.Range("c:c"))
End With
End If
End If
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯