excel VBA在工作表中点击ActiveX命令按钮弹出列表框 10
列表框的程序已经编好,并且可以在代码窗口中直接运行完成相应操作,如何通过点击左边的commonbutton按钮chartforsinglesupplier来触发这个列表框...
列表框的程序已经编好,并且可以在代码窗口中直接运行完成相应操作,如何通过点击左边的commonbutton按钮chart forsingle supplier 来触发这个列表框而不是在代码窗口中运行触发,如何编写程序代码呢。如图片所示
列表框的代码为:
Private Sub ListBox1_Click()
Dim i, j, flag As Integer
Dim wks As Worksheet
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "Failure analyse-" & ListBox1.Text
'复制黏贴
Worksheets("sheet1").Range("B6:B20").Copy
ActiveSheet.Paste Destination:=Worksheets("Failure analyse-" & ListBox1.Text).Range("A1:A15")
Worksheets("sheet1").Activate
Worksheets("sheet1").Range(Cells(6, (i + 3)), Cells(20, (i + 3))).Copy
Worksheets("Failure analyse-" & ListBox1.Text).Activate
ActiveSheet.Paste Destination:=Worksheets("Failure analyse-" & ListBox1.Text).Range("B1:B15")
'画图
Range("A1:B15").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Worksheets("Failure analyse-" & ListBox1.Text).Range("A1:B14")
ActiveChart.ChartType = xlColumnClustered
Range("A1:B15").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Worksheets("Failure analyse-" & ListBox1.Text).Range("A1:B14")
ActiveChart.ChartType = xlPie
ActiveChart.ApplyLayout (6)
End If
Next i
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
Dim X As String
X = ActiveWorkbook.Name
Me.Caption = "select a supplier "
ListBox1.List = Application.Transpose(Range("C6:AI6"))
End Sub 展开
列表框的代码为:
Private Sub ListBox1_Click()
Dim i, j, flag As Integer
Dim wks As Worksheet
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "Failure analyse-" & ListBox1.Text
'复制黏贴
Worksheets("sheet1").Range("B6:B20").Copy
ActiveSheet.Paste Destination:=Worksheets("Failure analyse-" & ListBox1.Text).Range("A1:A15")
Worksheets("sheet1").Activate
Worksheets("sheet1").Range(Cells(6, (i + 3)), Cells(20, (i + 3))).Copy
Worksheets("Failure analyse-" & ListBox1.Text).Activate
ActiveSheet.Paste Destination:=Worksheets("Failure analyse-" & ListBox1.Text).Range("B1:B15")
'画图
Range("A1:B15").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Worksheets("Failure analyse-" & ListBox1.Text).Range("A1:B14")
ActiveChart.ChartType = xlColumnClustered
Range("A1:B15").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Worksheets("Failure analyse-" & ListBox1.Text).Range("A1:B14")
ActiveChart.ChartType = xlPie
ActiveChart.ApplyLayout (6)
End If
Next i
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
Dim X As String
X = ActiveWorkbook.Name
Me.Caption = "select a supplier "
ListBox1.List = Application.Transpose(Range("C6:AI6"))
End Sub 展开
2个回答
展开全部
有两种方法实现你的要求,一种是button,一种是commandbutton。
先在文件中添加相应按钮,
如果使用的是button(按钮),可以先编制个接口程序比如
sub Main()
userform1.show
end sub
然后在button上右键,“指定宏”main给button即可。
如果使用的commandbutton,则需要在design model(设计模式)
下对commandbutton右键,选择查看代码,在
Private Sub CommandButton1_Click()
end sub
的end sub之前添加
userform1.show
commandbutton点击需要关闭设计模式。
以上代码中userform1需要改成程序中对应的窗体名字。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |