1

I create a chart in Excel using this VBA.

myRange = "Sheet1!$A1,ドルSheet1!$A3,ドルSheet1!$A8,ドルSheet1!$A13,ドルSheet1!$A18,ドルSheet1!$A23,ドルSheet1!$A28,ドルSheet1!$A34,ドルSheet1!$A41,ドルSheet1!$A48,ドルSheet1!$D1,ドルSheet1!$D3,ドルSheet1!$D8,ドルSheet1!$D13,ドルSheet1!$D18,ドルSheet1!$D23,ドルSheet1!$D28,ドルSheet1!$D34,ドルSheet1!$D41,ドルSheet1!$D48ドル"
 
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range( _
 myRange _
 )

When I add another couple of cells into the myRange variable (...,Sheet1!$A55ドル... ,Sheet1!$D55ドル), like this:

myRange = "Sheet1!$A1,ドルSheet1!$A3,ドルSheet1!$A8,ドルSheet1!$A13,ドルSheet1!$A18,ドルSheet1!$A23,ドルSheet1!$A28,ドルSheet1!$A34,ドルSheet1!$A41,ドルSheet1!$A48,ドルSheet1!$A55,ドルSheet1!$D1,ドルSheet1!$D3,ドルSheet1!$D8,ドルSheet1!$D13,ドルSheet1!$D18,ドルSheet1!$D23,ドルSheet1!$D28,ドルSheet1!$D34,ドルSheet1!$D41,ドルSheet1!$D48,ドルSheet1!$D55ドル"

I get this error:

Run-time error '1004'
Method 'Range' of object '_Global' failed

asked May 25, 2025 at 18:14

2 Answers 2

4

It's the limit of 255 characters. Use

myRange = "$A1,ドル$A3,ドル..."
 
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Sheet1.Range( _
 myRange _
 )

instead, where Sheet1 is the code name of "Sheet1".

answered May 25, 2025 at 18:20
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It is working now. How about if the sheet grows until I reach the 255 characters limit, even taking off 'Sheet1', from it?
In this case you need to optimize the references as per @CDP1802 answer, or use Union method, or put values (or their copies) into the continuous range like "X1:X10".
2
Dim rng as range
Set rng = Sheet1.Range("A1,A3,A8,A13,A18,A23,A28,A34,A41,A48,A55")
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Union(rng, rng.Offset(, 3))
answered May 25, 2025 at 19:21

1 Comment

Yes, the absolute address notation has no sense in this context. "Union"+"Offset" also adds more clarity.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.