
Label标签也类似
下面是网上一段类似动态添加控件的代码 供你参考
Private rowCount As Integer 'To keep track of rows.
Private Sub UserForm_Initialize()
rowCount = 1
End Sub
Private Sub LabelAddRow_Click() 'Event handler: Add row on click of label.
Dim tb As Control
rowCount = rowCount + 1
Set tb = Me.Controls.Add("Forms.TextBox.1", "textBox" &rowCount &"left", True) 'Add left TextBox.
With tb 'Position and size it:
.Top = 25 + (rowCount * 25) + 10
.Left = 25
.Height = 25
.Width = 100
End With
Set tb = Me.Controls.Add("Forms.TextBox.1", "textBox" &rowCount &"right", True) 'Same for the right one:
With tb
.Top = 25 + (rowCount * 25) + 10
.Left = 150
.Height = 25
.Width = 100
End With
LabelAddRow.Top = LabelAddRow.Top + 25 'Move the label down.
End Sub
希望可帮到你
1.数据做好,在旁边增加一列辅助列,并输入如图公式。
2.公式下拉,报表数据有多少行,就拉到多少行。
3.工具栏-开发工具-插入-组合框。
4.在工作表中画出控件,右击控件,设置控件格式。
5.如图设置,先把报表的各类名称依次排列下来。
6.选中辅助列数据-插入图表-三维柱形图。
7.右击选中控件-单击按住控件移到图表内,再进行图表美化,就成了。
希望楼主耐心揣摩,举一反三Sub 建立窗体并运行()
Dim TempForm As Object '采用后期绑定
Application.VBE.MainWindow.Visible = False '防止窗口闪动
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3) ' 建立窗体
With TempForm
.Properties("Caption") = "智能输入"
.Properties("Width") = 100
.Properties("Height") = 100
End With
With TempForm.Designer.Controls.Add("forms.ComboBox.1") '创建组合框
.Left = 10
.Top = 15
End With
With TempForm.Designer.Controls.Add("forms.CommandButton.1") '创建一个按钮
.Left = 10
.Top = 45
.Caption = "输入当前日期"
End With
With TempForm.CodeModule ' 为窗体添加代码
.InsertLines .CountOfLines + 1, "Private Sub UserForm_Activate()"
.InsertLines .CountOfLines + 2, "Me.ComboBox1.List = Array(""一月"", ""二月"", ""三月"", ""四月"", ""五月"", ""六月"", ""七月"", ""八月"", ""九月"", ""十月"", ""十一月"", ""十二月"")"
.InsertLines .CountOfLines + 3, "Me.ComboBox1 = WorksheetFunction.Text(VBA.Month(Date), ""[DBNum1][$-804]0月"")"
.InsertLines .CountOfLines + 4, "End Sub"
.InsertLines .CountOfLines + 5, "Private Sub ComboBox1_Change()"
.InsertLines .CountOfLines + 6, "If TypeName(ActiveCell) = ""Range"" Then ActiveCell = Me.ComboBox1.Text"
.InsertLines .CountOfLines + 7, "End Sub"
.InsertLines .CountOfLines + 8, "Private Sub CommandButton1_Click()"
.InsertLines .CountOfLines + 9, "If TypeName(ActiveCell) = ""Range"" Then ActiveCell = date"
.InsertLines .CountOfLines + 10, "End Sub"
End With
VBA.UserForms.Add(TempForm.Name).Show ' 显示窗体
ThisWorkbook.VBProject.VBComponents.Remove TempForm ' 运行完毕删除窗体
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)