数组中的类型不匹配

用户名

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

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数组是2D数组

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章