Question posted 2012 · +5 upvotes
I’m using vba for Excel in order to save data into a array by:
dim allPosts As Variant
allPosts = Range("A2:J5000")
after that I’m changing data in the allPosts array, and than I want to paste it back by:
Range("A2:J5000").Value = allPosts
I’m getting the error:
run time error 1004 Application-defined or object-defined
and the copy stops at a specific cell, when i change the string at this cell to be shorter the problem is solved.
thanks
Accepted answer +12 upvotes
You can use a second array to store the cell lengths that are too long, and separately iterate over these
From this Microsoft Support Article excel-2003 can’t handle writing back array strings longer than 911 characters excel-2010 worked fine on my testing)
The code below:
- Sets up a major variant array that reads in the range
- Sets up a second blank variant of equal size to the first array
- Tests each part of the array for cell length of more than 911 characters and then either
- manipulates the shorter value in the first array, or,
- removes the value from the first array, and then writes it to the second array
- The first array is dumped in a single shot back to the range
- The second array is iterated cell by cell to dump back the other strings
code
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
Excel VBA objects referenced (4)
Application— Using events with the Application objectApplication— Working with Other ApplicationsRange— Refer to Cells by Using a Range ObjectRange— Delete Duplicate Entries in a Range
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.