VBA 改变多个单元格的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24635049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:44:41  来源:igfitidea点击:

VBA Changing Value of Multiple Cells

excelexcel-vbavba

提问by DeeWBee

I have a row of cells that depending on which option button I have selected, will display a certain range of values. It isn't based on any equations or any logic. I need to display a specific set of values across multiple cells.

我有一排单元格,根据我选择的选项按钮,将显示一定范围的值。它不基于任何方程或任何逻辑。我需要在多个单元格中显示一组特定的值。

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then Range("B3","AF3").Value = 

End Sub

How would I indicate multiple values across cells in this case? Would I put all my values into some sort of an array first and then just feed the array into the .Value? Thanks!

在这种情况下,如何跨单元格指示多个值?我会先将所有值放入某种数组中,然后将数组放入.Value? 谢谢!

回答by DeeWBee

The method I ended up using is as follows.

我最终使用的方法如下。

Worksheets("Sheet1").Range("B3","AF3").Value = Worksheets("Sheet2").Range("B3","AF3").Value

回答by sous2817

There is probably a more elegant way, but here is one way to go about it:

可能有一种更优雅的方法,但这是一种方法:

Sub test()

Dim strArray(0 To 2)    As String
Dim rng                 As Range
Dim cell                As Range
Dim counter             As Long

Set rng = Range("A3,B7,G5")
counter = 0

strArray(0) = "Value1"
strArray(1) = "Value2"
strArray(2) = "Value3"

For Each cell In rng
    cell.Value = strArray(counter)
    counter = counter + 1
Next cell

End Sub