我是这么声明的
Public arr As Variant
在一个过程里赋值
Sub test()
arr = {"饮料","2101","2501"; "食品","2102","2601" ; "蔬菜","2103","2701"}
End Sub
然后,就 直报错,哪里不对吗?
我想让这个二维数组成这个样子(三行两列):
"饮料" "2101","2501"
"食品" "2102","2601"
"蔬菜" "2103","2701"
应该怎么声明和赋值啊?
字符串里的引号也要保留.
VBAä¸å¨æå®ä¹ä¸ä¸ªäºç»´æ°ç»ï¼æ¡ä¾ä»£ç å¦ä¸ï¼
//å®ä¹å¨ææ°ç»ttSub tt()
Dim a() As Integer
ReDim a(1 To 2, 1 To 1) As Integer
a(1, 1) = 10
i = 10
ReDim Preserve a(1 To 2, 1 To i) As Integer
MsgBox a(1, 1)
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2016-07-29
这样写:
arr = [{"饮料", "2101", "2501"; "食品", "2102", "2601"; "蔬菜", "2103", "2701"}]追问
arr = [{"饮料", "2101", "2501"; "食品", "2102", "2601"; "蔬菜", "2103", "2701"}]追问
好像不行啊.
追答如何报错的?最好贴个图。我专门试了一下,是对的。数组赋值,两端加方括号。
下图为测试结果:
我想要的结果是这样的,都带有引号,是个三行二列的数组.
arr(1)
arr(1,1)"饮料"
arr(1,2)"2101","2501"
arr(2)
arr(2,1)"食品"
arr(2,2)"2102","2601"
arr(3)
arr(3,1)"蔬菜"
arr(3,2)"2103","2701"
亲,看一下是这个意思不:
arr = [{"""饮料""", """2101"", ""2501""";"""食品""", """2102"", ""2601"""; """蔬菜""", """2103"", ""2701"""}]
第2个回答 2020-01-19
你对ubound函数的理解没错。问题在于数组a的结构只是你自己主观想象。
你在vba编辑器中,选择视图→本地窗口,然后按f8逐行测试代码,自然可以在本地窗口中看到数组a的结构。这样不就一目了然了,知道问题在哪儿了吗。
你在vba编辑器中,选择视图→本地窗口,然后按f8逐行测试代码,自然可以在本地窗口中看到数组a的结构。这样不就一目了然了,知道问题在哪儿了吗。
第3个回答 2018-05-09
试一试: 把 {"饮料","2101 。。。} 用 [ ] 围起来。
arr = [ {"饮料","2101","2501"; "食品","2102","2601" ; "蔬菜","2103","2701"} ]
arr = [ {"饮料","2101","2501"; "食品","2102","2601" ; "蔬菜","2103","2701"} ]