6

Problem - I have around more than 8202 characters in once cell say Range("A1").

Now I would like to copy the content of cell(A1) to cell(A2) using VBA. I'm using below Code

Sheets("XYZ").Range("A2") = Sheets("XYZ").Range("A1")

After the execution of the Code. It gives "Application Defined Or Object Defined Error !!"

Please help/assist with your expert comments.

Observation - If I reduce the length of "A1" cell to 8202 or less then about code works!

I'm Confused. Pls assist.

Community
  • 1
  • 1
Solution Seeker
  • 595
  • 4
  • 9
  • 16
  • As per reference for Excel 2010: "Total number of characters that a cell can contain: 32,767 characters" (http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010342495.aspx) What version of Excel are you using? Also, check for a special char in the cell. Regards, – Alexander Bell Nov 23 '14 at 04:49
  • @AlexBell - I am using Excel 2010 only. Don't know why its restricting me to 8202 character only. – Solution Seeker Nov 23 '14 at 05:00
  • As I told you, check for special characters at that position. Also, to start with: build a test string of just alphanumerics longer than 8202 char and see how it works. Regards, – Alexander Bell Nov 23 '14 at 05:03
  • @AlexBell - I don't have any special character in my cell. I've repeated alphabet "a" 8202 times & tried to copy the cell. It's giving error. – Solution Seeker Nov 23 '14 at 05:09
  • Is this the only line in your entire code block? The error may happen on other statements. Also, make sure that you have removed a cursor from the cells before running the Sub. I have tested it on 19,000 chars and it works fine. Regards – Alexander Bell Nov 23 '14 at 05:20

4 Answers4

5

Change your code to

Sheets("XYZ").Range("A2") = Sheets("XYZ").Range("A1").Value

and it will work.

Not really sure why though, as .Value is the default property of a range.

chris neilsen
  • 52,446
  • 10
  • 84
  • 123
  • The error most likely was caused by a cursor in the source cell (editing mode). Otherwise, it works fine without explicitly adding .value (it's a default property of range object as mentioned above by Chris Neilsen). Regards, – Alexander Bell Nov 23 '14 at 05:27
  • 1
    Sorry @alex but you are wrong here. Both Portland and I can replicate the issue. If it works for you, please post details of your setup and test code. Mine is Excel 2010, 32 bit, on Windows 7 64 bit. – chris neilsen Nov 23 '14 at 05:29
  • Yes, folks, you are right. I am trying it again and it failed without explicit ".value" property as stated by Chris Neilsen. The possible explanation is that the assignment operator could be confused with ambiguity: to use either value (property of the Range object), or just assign the object (Range) itself... just a guess. But still, this threshold of 8202 char is strange (cuz the same ambiguity exists for a smaller string). Regards, – Alexander Bell Nov 23 '14 at 05:45
  • (continued): just to test this hypothesis, I've modified the Sub using explicit Range object var assignment and it demonstrated exactly same behavior; Sub Test8202() Dim rng As Range Set rng = Sheets("XYZ").Range("A1") Sheets("XYZ").Range("A2") = rng End Sub – Alexander Bell Nov 23 '14 at 05:54
2

I was able to duplicate your error with the following:

Sub Test8202Copy()
    Dim wks As Worksheet
    Set wks = Worksheets("Sheet1")
    Dim x As String

    For i = 0 To 8202
        x = x + "a"
    Next i

    wks.Range("A1").Value = x
    wks.Range("A2") = wks.Range("A1")
End Sub

I was able to solve the error by adding .value to the copy.

Sub Test8202Copy()
    Dim wks As Worksheet
    Set wks = Worksheets("Sheet1")
    Dim x As String

    For i = 0 To 8202
        x = x + "a"
    Next i

    wks.Range("A1").Value = x
    wks.Range("A2").Value = wks.Range("A1").Value
End Sub

Using an intermediate variable without the use of .Value seems to work:

Dim y As Variant
y = wks.Range("A1")
wks.Range("A2") = y

My guess so far is that 8202 exceed the character limit of the data type used when you don't define .Value. The in cell limit length is 32,767 (MS Excel 2010) which is almost 4x the 8201 value that clears.

Automate This
  • 30,726
  • 11
  • 60
  • 82
  • Snap. Any insights into _why_? – chris neilsen Nov 23 '14 at 05:23
  • @chrisneilsen Check out my edit, interestingly enough an intermediate variable solves it too... hmmm Still chewing on this one. – Automate This Nov 23 '14 at 05:55
  • Yes, @PortlandRunner, you are right. But if you use Set y = wks.Range("A1") it will fail, as in my example with Range object assignment. It seems like the original expression (Sheets("XYZ").Range("A2") = Sheets("XYZ").Range("A1")) assigns Range Objects instead of .value property. – Alexander Bell Nov 23 '14 at 06:01
  • @AlexBell curious... Why would you use Set when copying a value to a variant? – Automate This Nov 23 '14 at 15:47
1

@Chris Neilsen provided the most practical and elegant solution to the problem (his code snippet follows):

Sheets("XYZ").Range("A2") = Sheets("XYZ").Range("A1").Value

In order to investigate and understand the possible cause of this strange behavior (may be a bug) of the Range object, I've posted couple comments, which are summarized below:

There is a conceptual difference between the original expression (see below):

Sheets("XYZ").Range("A2") = Sheets("XYZ").Range("A1")

and solution proposed by @Chris Neilsen, namely: original expression is implicitly assigning the Range object var (essentially, a pointer) to another Range object, like demonstrated in the following code snippet with explicit assignment:

Set rng = Sheets("XYZ").Range("A1")
Sheets("XYZ").Range("A2") = rng

while proposed solution explicitly passes the value property. Still, the reason why assigning a Range object failed for a value with string.Length>8202 is currently unclear (it may be caused by some internal nuances of the Excel Range object implementation).

Many thanks for posting this interesting question and fruitful discussion. Regards,

Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
1

This limit (see below re ) is covered in this MSDN article although interestingly it implies a vba array is invoved

Separately as per https://stackoverflow.com/a/13665363/641067 cant handle array strings longer than 911 characters, whereas the article below references 1823 character

SYMPTOMS

When you run a Microsoft Visual Basic for Applications (VBA) macro to transfer data from a VBA array that contains strings of data to a range of cells in a Microsoft Excel worksheet, the data may be truncated (cut off).

Note In Microsoft Office Excel 2003 and in later versions of Excel, you may receive the following error message when you run the VBA macro in the Visual Basic Editor: Run-time error '1004'

CAUSE

This problem may occur when one of the following conditions is true:

  • In Excel 2007, the VBA array is longer than 8,203 characters in length.
  • In Excel 2003 and in earlier versions of Excel, the VBA array is longer than 1,823 characters in length
Community
  • 1
  • 1
brettdj
  • 54,857
  • 16
  • 114
  • 177