|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
VBA窗体操作Excel形状示例
代码
- Sub 窗体初始化()
- Dim sp As Shape
- Dim i As Integer
-
- '移除原有选项
- For i = Me.图形列表.ListCount - 1 To 0 Step -1
- Me.图形列表.RemoveItem i
- Next
-
- '添加新选项
- For i = 1 To ThisWorkbook.Sheets("sheet1").Shapes.Count
- Set sp = ThisWorkbook.Sheets("sheet1").Shapes(i)
- Me.图形列表.AddItem sp.Name
- Next
- Me.图形列表.Style = fmStyleDropDownList
- Me.图形列表.Text = Me.图形列表.List(0, 0)
-
- Set sp = Nothing
- End Sub
- Sub 生成结果()
- Dim i As Long
- Dim sht As Worksheet
- Dim sp As Shape
-
- Set sht = ThisWorkbook.Sheets("sheet1")
- sht.Shapes(Me.图形列表.Text).Copy
-
- i = 2
- Do While sht.Range("a" & i).Value <> ""
- 复制图形并赋值 Me.图形列表.Text, sht.Range("e" & i), sht.Range("a" & i).Value
- i = i + 1
- Loop
-
- Set sht = Nothing
- Set sp = Nothing
- End Sub
- Sub 复制图形并赋值(shapeName As Variant, Rng As Range, spText)
- ActiveSheet.Shapes.Range(Array(shapeName)).Select
- Selection.Copy
- Range(Rng.Cells(1).Address).Select
- Rng.Worksheet.Paste
- Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = CStr(spText)
- End Sub
- Private Sub CommandButton1_Click()
- 生成结果
- End Sub
- Private Sub UserForm_Initialize()
- 窗体初始化
- End Sub
- Private Sub 图形列表_Change()
- ThisWorkbook.Sheets("Sheet1").Shapes(Me.图形列表.Text).Select
- End Sub
复制代码
|
-
-
示例文件.zip
20.4 KB, 下载次数: 22
打开启动窗体,操作查看效果。
|