【2】VBA变量名动态变量 1)EXCEL VBA 里面如何动态获取字符串转换为变量名? https://zhidao.baidu.com/question/1048303865908582659.html https://zhidao.baidu.com/question/1048303865908582659.html 我有变量 st1 = "wew " st2="were" st3="erwer"...st53="erere" 共53个变量,想通过 一个for 循环来完成以下工作(清空每一个变量值) for x=1 to 53 "st"& x="" '在excel2016中不行? x&i=i '在excel2016中不行? next x -------- 2#/BBS:A.最好别用上述这种方式赋值,可用St(1 to 53)这样的数组进行赋值?方便、简单。B.可用CallByName取值,以及赋值,取值操作: 将变量定义成Public类型 取值可以用CallByName, For x=1 To 53 Debug.Print CallByName(Me, "st" & x, VbGet) Next x 赋值操作: 同样要将变量定义成Public For x=1 To 53 CallByName Me, "st" & x, VbLet, "" Next x 可以msgBox st1 试试是否更改了值 .......... BBS,3#/不行,但是可以用字典过渡 dim dic as object set dic=createobject("scripting.dictionary") for x=1 to 53 dic("st" & x)="" next 调用时,如想知道st5 msgbox dic("st5") -------- 见VBA字符串数组 Sub yb动态变量赋值() Dim StrArray1(1 To 2) As String StrArray1(1) = "销售收入" StrArray1(2) = "营业收入" '调用数组值 Dim Str As String Str = StrArray1(2) '取数组第2个值 MsgBox Str |
|
来自: c857084163 > 《Excel》