gpt4 book ai didi

excel - 范围不会更改多个单元格

转载 作者:行者123 更新时间:2023-12-04 21:30:33 25 4
gpt4 key购买 nike

Sub ButtonLower()

Range("b2", "b7").Value = Range("b2", "b7").Value - 2

End Sub
Sub ButtonHigher()

Range("b2", "b7").Value = Range("b2", "b7").Value + 2

End Sub

最终结果是只有 B2的值变化,而不是整个范围。有人可以帮我弄这个吗?

最佳答案

您不能直接对整个数组进行数学运算。使用评估:

With ActiveSheet.Range("B2:B7")
.Value = ActiveSheet.Evaluate(.Address & " - 2")
End With


With ActiveSheet.Range("B2:B7")
.Value = ActiveSheet.Evaluate(.Address & " + 2")
End With

或者您可以将整个加载到一个变体数组中并循环该数组:
Sub ButtonLower()
Dim arr() As Variant
arr = ActiveSheet.Range("b2:b7").Value

Dim i As Long
For i = LBound(arr, 1) To UBound(arr, 1)
Dim j As Long
For j = LBound(arr, 2) To UBound(arr, 2)
arr(i, j) = arr(i, j) - 2
Next j
Next i

ActiveSheet.Range("b2:b7").Value = arr
End Sub

关于excel - 范围不会更改多个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53561513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com