我有一个旋转按钮,分成两个事件,SpinUp&SpinDown。我希望用户选择任何给定的单元格范围,然后使用自旋按钮,将单元格的值增加或减少1。这是我目前的代码:
Private Sub SpinButton1_SpinUp()
Dim myRange As Range
Set myRange = Selection
SpinButton1.Height = 45
SpinButton1.Width = 39
SpinButton1.Left = 283.5
SpinButton1.Top = 328.5
myRange.Value = myRange.Value + 1
'(assume -1 for the SpinDown function)
End Sub当我一次选择多个单元格时,会得到一个运行时错误,说明类型不匹配。我是非常新的VBA/Excel编程,所以任何帮助都是非常感谢的。谢谢!
发布于 2014-07-10 15:26:23
一种方法是检查是否选择了多个单元格。类似于:
Sub test()
Dim myRange As Range
Set myRange = Selection
If myRange.Cells.Count > 1 Then Exit Sub
MsgBox "Only 1 cell selected"
End Sub如果要操作多个单元格,请尝试:
Private Sub SpinButton1_Change()
With Application
.ScreenUpdating = False
End With
Dim myRange As Range
Dim myCell As Range
Set myRange = Selection
SpinButton1.Height = 45
SpinButton1.Width = 39
SpinButton1.Left = 283.5
SpinButton1.Top = 328.5
For Each myCell In myRange
myCell.Value = myCell.Value + 1
Next myCell
With Application
.ScreenUpdating = True
End With
End Subhttps://stackoverflow.com/questions/24680163
复制相似问题