本文介绍了如何在Excel VBA中重新创建变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在写一个excel宏。在这里,我试图在循环内重新创建变量名称,以避免多行几乎相同的代码。但我的问题是重新创建的变量转为字符串,我无法获得分配给该变量的值。请帮助!! 我尝试过: 昏暗天数,百分比As Variant days = Array(015,1530,3160,6190,g90) percent = Array(7080, 8090,g90) 对于i = 0到4 对于j = 0到2 v = cnt7080_015 valu =cnt &百分比(j)& _& days(i) Cells(rowc,k).Value =cnt&百分比(j)& _&天(i) rowc = rowc + 1 下一个k = k + 1 rowc = 7 下一个 解决方案 在稍作修改后查看代码: Dim 天作为 变体,百分比作为 变体,sTmp 作为 字符串 Dim wsh 作为工作表' 声明变量它引用Sheet1 ' 启动工作表类型的变量 ' 更改它符合您的需求 设置 wsh = ThisWokbook.Worksheets( Sheet1) days = Array( 015, 1530, 3160, 6190 , g90) percent = Array( 7080, 8090, g90) 对于 i = LBound(天)到 UBound(天) 对于 j = LBound(每个分) UBound(百分比) ' v = cnt7080_015 sTmp = cnt&百分比(j)& _&天(i) wsh.Cells(rowc,k).Value = sTmp rowc = rowc + 1 下一步 k = k + 1 rowc = 7 下一步 ' 清理! 设置 wsh = 没什么 我强烈建议您阅读:编码技巧和编程实践 [ ^ ] Excel VBA编程 - 多维数组 [ ^ ] Hi,I am writing an excel macro. Here I am trying to recreate the variable names inside loop to avoid multiple lines of almost same codes. But my problem is that the recreated variable is turning as string and I am unable to get the value assigned to that variable. Please help!!What I have tried:Dim days, percent As Variant days = Array("015", "1530", "3160", "6190", "g90") percent = Array("7080", "8090", "g90") For i = 0 To 4 For j = 0 To 2 v = cnt7080_015 valu = "cnt" & percent(j) & "_" & days(i) Cells(rowc, k).Value = "cnt" & percent(j) & "_" & days(i) rowc = rowc + 1 Next k = k + 1 rowc = 7 Next 解决方案 Excel VBA Programming - Multidimensional Arrays[^] 这篇关于如何在Excel VBA中重新创建变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 13:47