动态添加基于工作表名称的新表单控件复选框(Excel VBA)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态添加基于工作表名称的新表单控件复选框(Excel VBA)相关的知识,希望对你有一定的参考价值。
如果有任何新工作表添加到工作簿,我正在尝试创建一个添加复选框(单击按钮)的系统。我的代码在下面,它能够创建复选框,直到我尝试更改位置,所以我假设它不起作用的原因是由于这一点。
Private Sub Update_Click()
Dim cb As CheckBox
Dim Exists As Boolean
'I think these location/ dimension variables are perhaps wrong (I'm not sure what values they take)
Dim TopLocation As Double
Dim LeftLocation As Double
Dim Width As Double
Dim Height As Double
For Each ws In ActiveWorkbook.Worksheets
'This loop is simply to stop it from making duplicate checkboxes
Exists=False
For Each cb In ThisWorkbook.Worksheets("Summary").CheckBoxes
If cb.name = ws.name Or ws.name = "Summary" Or ws.name = "Price List (2)" Then
Exists = True
End If
Next
If Exists = False Then
TopLocation = 0
LeftLocation = 0
Width = 0
Height = 0
'The following loop is an attempt to find the checkbox that is furthest down the page, the problem is that I am not too familiar with the location attribute so am just assuming that it increases as you move down the page
For Each cb In ThisWorkbook.Worksheets("Summary").CheckBoxes
If cb.Top > TopLocation Then
TopLocation = cb.Top
End If
If cb.Left > LeftLocation Then
LeftLocation = cb.Left
End If
If cb.Width > Width Then
Width = cb.Width
End If
If cb.Height > Height Then
Height = cb.Height
End If
Next
'The following is where I believe the problem to be, I thought that I could simply use the variables I had created to place the new one in this location
With ThisWorkbook.Worksheets("Summary").CheckBoxes.Add(LeftLocation, TopLocation + Height, Width, Height)
.name = ws.name
.Caption = ws.name
End With
End If
Next ws
End Sub
我想也许这是对复选框语法的误解,并希望有人能帮助我理解我哪里出错了。任何帮助表示赞赏,谢谢:)
答案
你有两个错别字..
在线
With ThisWorkbook.Worksheets("Summary").CheckBoxes.Add(LocationLeft, LocationTop + Height, Width, Height)
变量应该是LeftLocation(不是LocationLeft)和TopLocation(不是LocationTop)
祝好运
以上是关于动态添加基于工作表名称的新表单控件复选框(Excel VBA)的主要内容,如果未能解决你的问题,请参考以下文章