将数组复制到多个范围
Copy Array to Multiple Range
第一次在这里发帖。我正在尝试将包含 10 个元素的数组复制到 10 个单独的范围。问题是所有单元格都只有数组中的第一个元素。在下面的示例中,我只得到 ON。我在第 3 到第 5 个范围内没有得到 OFF。
编辑我试图在不使用循环的情况下执行此操作,因为我可能有很多元素并且在复制过程中速度变慢。
Dim Rng As String
Dim OnOff() As Variant
Rng = "F7,G4,H3,I9,J8,K2,L10,M1,N6,O5"
OnOff = Array("ON", "ON", "OFF", "OFF", "OFF", "ON", "ON", "ON", "ON", "ON")
Range(Rng).Value = OnOff
这就是我得到的:
这是我希望得到的:
根据您发布的图片,您只想同时遍历 Rng
和 OnOff
Dim Rng As String
Dim OnOff() As Variant
Rng = "F7,G4,H3,I9,J8,K2,L10,M1,N6,O5"
OnOff = Array("ON", "ON", "OFF", "OFF", "OFF", "ON", "ON", "ON", "ON", "ON")
Dim i As Long
Dim v As Variant
v = Split(Rng, ",")
For i = LBound(OnOff) To UBound(OnOff)
Range(v(i)) = OnOff(i)
Next i
由于似乎需要写入非连续范围,因此可以使用此 link 中的代码来加快速度。
写入非连续范围
- 尝试创建两个单独的单元格地址字符串。
Const rgOnAddress As String = "F7,G4,L10,M1,N6,O5"
Const rgOffAddress As String = "H3,I9,J8"
Range(rgOnAddress).Value = "ON"
Range(rgOffAddress).Value = "OFF"
第一次在这里发帖。我正在尝试将包含 10 个元素的数组复制到 10 个单独的范围。问题是所有单元格都只有数组中的第一个元素。在下面的示例中,我只得到 ON。我在第 3 到第 5 个范围内没有得到 OFF。 编辑我试图在不使用循环的情况下执行此操作,因为我可能有很多元素并且在复制过程中速度变慢。
Dim Rng As String
Dim OnOff() As Variant
Rng = "F7,G4,H3,I9,J8,K2,L10,M1,N6,O5"
OnOff = Array("ON", "ON", "OFF", "OFF", "OFF", "ON", "ON", "ON", "ON", "ON")
Range(Rng).Value = OnOff
这就是我得到的:
这是我希望得到的:
根据您发布的图片,您只想同时遍历 Rng
和 OnOff
Dim Rng As String
Dim OnOff() As Variant
Rng = "F7,G4,H3,I9,J8,K2,L10,M1,N6,O5"
OnOff = Array("ON", "ON", "OFF", "OFF", "OFF", "ON", "ON", "ON", "ON", "ON")
Dim i As Long
Dim v As Variant
v = Split(Rng, ",")
For i = LBound(OnOff) To UBound(OnOff)
Range(v(i)) = OnOff(i)
Next i
由于似乎需要写入非连续范围,因此可以使用此 link 中的代码来加快速度。
写入非连续范围
- 尝试创建两个单独的单元格地址字符串。
Const rgOnAddress As String = "F7,G4,L10,M1,N6,O5"
Const rgOffAddress As String = "H3,I9,J8"
Range(rgOnAddress).Value = "ON"
Range(rgOffAddress).Value = "OFF"