作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个将形状添加到工作表的命令按钮。每次按下 CommandButton 时,我都希望计数增加一。我还希望将此计数值应用于形状。例如,如果 CommandButton 已按下一次,我希望形状包含 text.character "1",按下两次,我希望形状包含 text.character "2",依此类推。
我还希望能够将计数器重置为特定值。例如,如果形状 #14 被删除并且命令按钮计数为 16,则需要将计数调整回 14,以便通过命令按钮创建另一个形状 #14。这也可用于将计数重置为 0。
本质上,我想要一个跟踪命令按钮点击次数的计数器。然后可以将此数字分配给全局变量,该变量稍后可以由计数器更新或由用户输入操作。
下面是与添加形状和 CommandButton 点击计数器相关的所有代码。
这是在 Sheet1 中:
Private Sub CommandButton2_Click() 'Add Shape to Picture, index click
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
CountWelds CommandButton2
Call ShapeWithNum
End Sub
Option Explicit
Dim wb As Workbook
Dim ws As Worksheet
Public buttonCell As Range
Sub CountWelds(WeldControl As MSForms.CommandButton) 'Counter of clicks
Set buttonCell = WeldControl.TopLeftCell
buttonCell = buttonCell + 1
buttonCell.Offset(0, 1).Value = buttonCell & " visitors from " & WeldControl.name & "."
End Sub
Sub Set_buttonCellCount() 'Set the counter to a specific value
Dim answer As Long
CountWelds ThisWorkbook.Sheets("Sheet1").CommandButton2
answer = InputBox("Choose Weld Number i.e. 1, 2, 3")
buttonCell = answer
MsgBox "Weld Number set to " & buttonCell + 1
End Sub
Sub ShapeWithNum() 'Sub which adds the shape to sheet 1
Dim weldw, weldl As Variant
CountWelds ThisWorkbook.Sheets("Sheet1").CommandButton2
If buttonCell < 10 Then
weldw = 20
weldl = 20
Else
weldw = 40
w eldl = 20
End If
Index errors occur at each of the "Selection." locations.
ActiveSheet.Shapes.AddShape(msoShapeOval, 650, 100, weldw, weldl).Select
Selection.ShapeRange.TextFrame2.TextRange.ParagraphFormat.Alignment = _
msoAlignCenter
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = buttonCell
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 0). _
ParagraphFormat
.FirstLineIndent = 0
.Alignment = msoAlignLeft
End With
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Font
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.ObjectThemeColor = msoThemeColorLight1
.Fill.ForeColor.TintAndShade = 0
.Fill.ForeColor.Brightness = 0
.Fill.Transparency = 0
.Fill.Solid
.Size = 11
.name = "+mn-lt"
End With
MsgBox buttonCell 'Used to see if the ButtonCell is indeed indexing
'CountWelds ThisWorkbook.Sheets("Sheet1").CommandButton2
最佳答案
这是一个简单的例子,假设一个 ActiveX 按钮
Private Sub CommandButton1_Click()
Dim s As Shape, n As Long, s1 As Shape
For Each s1 In ActiveSheet.Shapes
If s1.AutoShapeType = msoShapeOval Then n = n + 1
Next s1
n=n+1
Set s = ActiveSheet.Shapes.AddShape(msoShapeOval, 650, 100 + n * 50, 50, 50)
s.TextFrame.Characters.Text = n
s.TopLeftCell.Offset(1, 2).Value = "This is button press number " & n
End Sub
关于excel - 命令按钮点击计数器,可调整并链接到形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57927237/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!