arrays - 数组中的类型不匹配

标签 arrays vba excel

我有这段代码将所选范围的值存储在数组中。

dim lRow, i as Integer
dim rngValues() as Variant

rngValues = thisworkbook.Sheets(2).Range("C2:C" & lRow)


For i = LBound(rngValues) To UBound(rngValues)
    Debug.Print rngValues(i) ' this lines returns Type Mismatch error.
Next i

对列进行一些修改后,我必须将其粘贴回列。请您帮我解决这个问题吗?

最佳答案

编辑以返回特定格式的日期值

正如 Tim Williams 已经说过的,您的 rngValues 数组是二维数组

要将其作为一维数组,您可以编写如下代码:

dim lRow A Long, i as Long '<--| expliciltly declare each variable and use Long for row index ones since they can exceed Integer capacity
dim rngValues as Variant '<--| declare rngValues as a Variant instead as an array of Variant

rngValues = Application.Transpose(ThisWorkbook.Sheets(2).Range("C2:C" & lRow).Value) '<--| transpose the "columned" values to "rowed" ones suitable for a 1D array

For i = LBound(rngValues) To UBound(rngValues)
    Debug.Print Format(rngValues(i),"yyyy/mm/dd")
Next

关于arrays - 数组中的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42266099/

相关文章:

c - 求char**形式的字符串数组的长度?

excel - 关于设置相同值并查找相同值的VBA问题

excel - VBA - 合并其他工作表时忽略工作表

javascript - 三个数字的最大值 DOM 操作

java - 在控制台上打印数组引用变量

html 到 excel 转换为嵌入图像

android - 使用 Apache poi 和 MS Excel 在 Android 应用程序中插入图像时出现 NoClassDefFoundError

php - 一起使用 php 和 python

excel - 根据另一个工作簿行的编号复制和粘贴

php - 从数组构建插入查询