我正在为 Excel 使用vba,以便通过以下方式将数据保存到数组中:
Dim allPosts As Variant
allPosts = Range("A2:J5000")
之后,我正在更改allPosts数组中的数据,然后我想通过以下方式将其粘贴回来:
Range("A2:J5000").Value = allPosts
我收到错误:
运行时错误 1004 应用程序定义或对象定义
并且复制在特定单元格处停止,当我将此单元格处的字符串更改为更短时,问题就解决了。
谢谢
我正在为 Excel 使用vba,以便通过以下方式将数据保存到数组中:
Dim allPosts As Variant
allPosts = Range("A2:J5000")
之后,我正在更改allPosts数组中的数据,然后我想通过以下方式将其粘贴回来:
Range("A2:J5000").Value = allPosts
我收到错误:
运行时错误 1004 应用程序定义或对象定义
并且复制在特定单元格处停止,当我将此单元格处的字符串更改为更短时,问题就解决了。
谢谢
您可以使用第二个数组来存储太长的单元格长度,并分别迭代这些
从这篇 Microsoft 支持文章中 ,excel-2003无法处理写回长度超过911个字符的数组字符串, excel-2010在我的测试中运行良好)
下面的代码:
代码
Sub KudosRickyPonting()
Dim allPosts As Variant
Dim allPosts2 As Variant
Dim vStrs As Variant
Dim lngRow As Long
Dim lngCol As Long
allPosts = Range("A2:J5000").Value2
ReDim allPosts2(1 To UBound(allPosts, 1), 1 To UBound(allPosts, 2))
For lngRow = 1 To UBound(allPosts, 1)
For lngCol = 1 To UBound(allPosts, 2)
If Len(allPosts(lngRow, lngCol)) < 912 Then
allPosts(lngRow, lngCol) = "Updated:" & allPosts(lngRow, lngCol)
Else
allPosts2(lngRow, lngCol) = "NEW PART " & allPosts(lngRow, lngCol)
'erase long value from first array
allPosts(lngRow, lngCol) = vbNullString
End If
Next
Next
Range("A2:J5000").Value = allPosts
For lngRow = 1 To UBound(allPosts2, 1)
For lngCol = 1 To UBound(allPosts2, 2)
If Len(allPosts2(lngRow, lngCol)) > 0 Then Range("A2").Offset(lngRow - 1, lngCol - 1).Value2 = allPosts2(lngRow, lngCol)
Next
Next
End Sub