VBA : How to define a large set of string variables?

标签 vba excel

我正在尝试(在 VBA 中)定义大量字符串变量。蛮力将是:
Dim Port1 as String Dim Port2 as String etc…
说 100 个变量令人不快。必须有更智能的解决方案。
我努力了 :
Dim n As Integer For n = 1 To 100 Dim "Port" & n as String Next n
和它的变化没有成功。
如果有人能指出正确的方向或分享一个例子,我将不胜感激。

最佳答案

数组正是这样做的!看这个例子:

Dim Port(1 to 100) As String
Dim i As Long

Port(1) = "String"
For i = 2 to 100
    Port(i) = "String " & i
Next i

它将第一个数组元素 Port(1) 设置为单词“String”。其他一切Port(2)等等,包含“字符串 2”、“字符串 3”等。直到 Port(100) .

我希望这会有所帮助!

关于VBA : How to define a large set of string variables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322444/

相关文章:

VBA - 日期的最小值/最大值()

excel - 如何使用公式获取excel中不同行的值?

excel - 从 VBA Excel 宏调用后可执行文件未正确运行

vba - 使用Evaluate比较date类型和int类型

excel - 新问题 - 运行时错误 - 内存不足

database - excel中如何分离数据

excel - 重置Excel "Find and Replace"对话框参数

excel - 图表对象删除错误

excel - 通过 VBA 调用时,dec2bin 无法按预期工作

Excel 列到数组列表