我有一个旋转按钮,分为两个事件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编程的新手,所以任何帮助都表示赞赏。谢谢!
答案 0 :(得分:2)
一种方法是检查是否选择了多个单元格。类似的东西:
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 Sub