gpt4 book ai didi

vba - 如何在 Excel VBA 的 for 循环中调用数组中的命名范围?

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

我创建了一个数组,其中包含命名范围。

enter image description here

这是我的代码:

Sub Macro()
' - - - - - - - - - - - - - - - - -
Dim fruits As Variant
fruits = Array("Apple", "Banana", "Coconut")

For i = 1 To Length(fruits)

Cells(5, i).Select
ActiveCell.FormulaR1C1 = Range(fruits.Cells(i))(1)

Next i
End Sub

我想在 for 循环中从数组中调用这些命名范围。我怎样才能做到这一点?预先感谢您。

我想在第 5 行显示命名范围的值。

最佳答案

一维数组默认从零开始,而不是从一开始。

Sub Macro()
Dim fruits As Variant

fruits = Array("Apple", "Banana", "Coconut")

For i = lbound(fruits) to ubound(fruits)
Cells(5, i + 1) = Range(fruits(i))(1)
'maybe this
'Cells(5, i + 1) = Range(fruits(i)).cells(1, 2)
Next i
End Sub

关于vba - 如何在 Excel VBA 的 for 循环中调用数组中的命名范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50150114/

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