我有一个问题。
我已经按下一个按钮来选择3个单元格。现在,通过编写代码,我可以对活动单元格计数-1,对所选单元格计数+1。效果很好,但是我只想向所选的一个单元格添加+1,而另一个我想保持原样。
这是我到目前为止编写的代码
Sub AddNumber()
Dim ws As Worksheet
Dim rngSel As Range
Dim rng As Range
Dim Arr() As Variant
Set rngSel = ActiveCell
Set rngSel2 = Selection
Dim index As Integer
index = 0
For Each rng In rngSel2.Areas
If index = 0 Then
rng = rng - 2
Else
rng = rng - 0
End If
index = index - 2
If RangerngSel = rng Then
rng = rng + 0
End If
rng = rng - 0
If rng.Count = 1 Then
rng = rng + 1
Else
Arr = rng
rng.Value = Arr
End If
Next rng
End Sub
是否有代码取消选择我不想为E34单元格添加值的单元格?
非常感谢您的帮助!
答案 0 :(得分:0)
Sub AddNumber()
'Select a range of 3 cells
Range("C3:C5").Select
'Subtract 1 from the top cell's value
ActiveCell.Value = ActiveCell.Value - 1
'Activate the cell below the first and add 1 to its value
Cells(ActiveCell.Row + 1, ActiveCell.Column).Activate
ActiveCell.Value = ActiveCell.Value + 1
'Activate the third cell and add 2 to its value
Cells(ActiveCell.Row + 1, ActiveCell.Column).Activate
ActiveCell.Value = ActiveCell.Value + 2
End Sub
由于您只想保留其中一个单元格的值,因此可以省略代码块之一,只需相应地调整Cells(ActiveCell.Row + 1 [...]
部分即可。