Subscript out of range

H

Harley Feldman

I am trying to add code to a chart module that was created correctly with VB. I have tried both of the code sequences below, and I receive a "subscript out of range" error in both cases. Any suggestions on how to fix the problem?

With ActiveWorkbook.VBProject.VBComponents("DASH").CodeModule
StartLine = .CreateEventProc("Calculate", "Chart") + 1
.InsertLines StartLine, _
" Dim s as Series" & vbCr & _
" On Error GoTo NoSeries" & vbCr & _
" With ActiveChart" & vbCr & _
" For Each s in .seriescollection" & vbCr & _
" s.Border.Weight = xlThick" & vbCr & _
"NoErr:" & vbCr & _
" Next" & vbCr & _
" End With" & vbCr & _
" Exit Sub" & vbCr & _
"NoSeries:" & vbCr & _
" Resume NoErr" & vbCr & _
"End Sub"
End With

ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.ActiveChart.CodeName).CodeModule.AddFromString _
"Private Sub Chart_Calculate()" & vbCr & _
" Dim s as Series" & vbCr & _
" On Error GoTo NoSeries" & vbCr & _
" With ActiveChart" & vbCr & _
" For Each s in .seriescollection" & vbCr & _
" s.Border.Weight = xlThick" & vbCr & _
"NoErr:" & vbCr & _
" Next" & vbCr & _
" End With" & vbCr & _
" Exit Sub" & vbCr & _
"NoSeries:" & vbCr & _
" Resume NoErr" & vbCr & _
"End Sub"

Harley
 
O

Otto Moehrbach

Harley
When you receive the error message, it is in a dialog box. Along the
bottom of this box there are several buttons. One of them is the Debug
button. Click on this button. Excel will then take you to the code and
will highlight the offending line of code. Tell us what that line is.
The "subscript out of range" error message usually means that the code
is calling for something that doesn't exist. For instance, if you reference
or call for a sheet that doesn't exist. HTH Otto
I am trying to add code to a chart module that was created correctly with
VB. I have tried both of the code sequences below, and I receive a
"subscript out of range" error in both cases. Any suggestions on how to fix
the problem?

With ActiveWorkbook.VBProject.VBComponents("DASH").CodeModule
StartLine = .CreateEventProc("Calculate", "Chart") + 1
.InsertLines StartLine, _
" Dim s as Series" & vbCr & _
" On Error GoTo NoSeries" & vbCr & _
" With ActiveChart" & vbCr & _
" For Each s in .seriescollection" & vbCr & _
" s.Border.Weight = xlThick" & vbCr & _
"NoErr:" & vbCr & _
" Next" & vbCr & _
" End With" & vbCr & _
" Exit Sub" & vbCr & _
"NoSeries:" & vbCr & _
" Resume NoErr" & vbCr & _
"End Sub"
End With

ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.ActiveChart.CodeName).CodeModule.AddFromString
_
"Private Sub Chart_Calculate()" & vbCr & _
" Dim s as Series" & vbCr & _
" On Error GoTo NoSeries" & vbCr & _
" With ActiveChart" & vbCr & _
" For Each s in .seriescollection" & vbCr & _
" s.Border.Weight = xlThick" & vbCr & _
"NoErr:" & vbCr & _
" Next" & vbCr & _
" End With" & vbCr & _
" Exit Sub" & vbCr & _
"NoSeries:" & vbCr & _
" Resume NoErr" & vbCr & _
"End Sub"

Harley
 
H

Harley Feldman

Otto,

In the case of the first code set, only the first line is highlighted. In teh second case, the entire set of lines of code is highlighted as it is one line of code.

Another thing that I have noticed. If this code is integrated higher into my routine, and I execute an Exit Sub right after the last line of this routine, it works fine with no errors. If I take the Exit Sub out, then Excel aborts. The subscript out of range message occurs when I place these routines at the end of the sub procedure.

Harley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top