|
回复 5楼 小学童 的帖子
刚刚有个热心人帮我注释了,我复制来了给你看看!
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Row < 2 Then Exit Sub
If Target.Column > 1 Then Exit Sub
vl = Target.Value
If Len(vl) > 1 Then Exit Sub
Target.Select
On Error Resume Next
Set sj = Worksheets("Sheet2") ’这里就是菜单选项数据的来源工作薄,可以把"Sheet2"改成你想要的
ed = sj.[a65536].End(xlUp).Row '这里是从哪一列取数据,先找到这一列的最后一个有数据行
Set rg = sj.Range("a2:a" & ed).Find(vl, LookIn:=xlValues) ‘设置查找的范围,这里是在a列,从第二行到最后一个有数据的行,把这2个地方的a改成别的就可以从其它列里查找,后面还有一个
If rg Is Nothing Then Exit Sub
With Application.CommandBars.Add(Name:="mycell", Position:=msoBarPopup)
r0 = 1
r = rg.Row
Do While r > r0
r0 = r
With .Controls.Add(Type:=msoControlButton)
.Caption = rg.Value
.OnAction = "bbb"
End With
Set rg = sj.Range("a2:a" & ed).FindNext(rg) '这个也是查找数据的范围
If Not rg Is Nothing Then r = rg.Row
Loop
End With
If Application.CommandBars("Mycell").Controls.Count = 1 Then
Application.EnableEvents = False
rownum = Worksheets(3).[d65506].End(xlUp).Row + 1
Set ds = Worksheets(3)
Set xx = ds.Range("d" & rownum)
'这里设置要把数据输出到哪里,现在是第三个工作薄的d列,修改Worksheets(3)为其它的工作薄,d改成其它的列就可以,bbb进程里也一样的
xx.Value = Application.CommandBars("Mycell").Controls(1).Caption
Application.EnableEvents = True
Else
Application.CommandBars("Mycell").ShowPopup
End If
Application.CommandBars("Mycell").Delete
End Sub
Sub bbb()
Application.EnableEvents = False
rownum = Worksheets(3).[d65506].End(xlUp).Row + 1
Set ds = Worksheets(3)
Set xx = ds.Range("d" & rownum)
'这里也是设置输入区域的
xx.Value = Application.CommandBars.ActionControl.Caption
Application.EnableEvents = True
End Sub |
|