5

I'm using for Excel in order to save data into an array by:

Dim allPosts As Variant
allPosts = Range("A2:J5000")

after that I'm changing data in the allPosts array, and then 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

mielk
  • 3,890
  • 12
  • 19
Presen
  • 1,809
  • 4
  • 31
  • 46
  • 1
    Submit whatever you have. I don't know about the others but I have no crystal ball at hand. – ApplePie Dec 01 '12 at 20:28
  • the changes I'm performing on allPosts array are complicated, hence the built-in macro recorder didn't help – Presen Dec 01 '12 at 20:40
  • well, use recorder to see how paste function works; adapt it to your needs – Andrew Dec 01 '12 at 20:47
  • the paste function works, only when it reach the very long string cell it crash. thanks! – Presen Dec 01 '12 at 21:08
  • Have you tried just looping over your array and pasting it to destination cells one at the time? `http://msdn.microsoft.com/en-us/library/office/aa221353(v=office.11).aspx` – Andrew Dec 01 '12 at 21:40
  • @Andrew, the macro recorder is useful for basic mimicking of manual actions - using variant arrays is a programming technique with no manual equivalent. – brettdj Dec 02 '12 at 00:57

1 Answers1

12

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 can't handle writing back array strings longer than 911 characters worked fine on my testing)

The code below:

  1. Sets up a major variant array that reads in the range
  2. Sets up a second blank variant of equal size to the first array
  3. 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
  4. The first array is dumped in a single shot back to the range
  5. 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
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • 1
    thanks!!! is there a way to slice down strings that are longer than 911? This way I will run normally on allPosts, when a long string apear I will slice it down, and at last I will paste allPosts back to the Sheet normally by: Range("A2:J5000").Value = allPosts – Presen Dec 02 '12 at 15:27
  • 1
    Agree with Doug... Very Nice :) – Siddharth Rout Apr 26 '13 at 06:46
  • 1
    Uped for KudosRickyPonting – dilbert Aug 08 '13 at 12:35