ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error after last object in the array when looping For...each (https://www.excelbanter.com/excel-programming/318934-error-after-last-object-array-when-looping-each.html)

Gunnar Johansson[_3_]

Error after last object in the array when looping For...each
 
Hi,
I have named chartobjects grouped as an array "arGrp21" and when I run the
"For each chtobj..." the loop works ok until the last, "R_Q_F_21", is done.
After "R_Q_F_21" I get error with the line "Next" . Does I have to tell how
many chartobjects there is? How? Else: What is the error with my code?

/Regards

Code Simplified:

Sub ArrayList
Dim arGrp21() As Variant

arGrp21 = Array("R_CF_FB_21", "R_ROI_FB_21", "R_Q_FB_21", "R_CF_FBA_21",
"R_ROI_FBA_21", "R_Q_FBA_21", "R_CF_F_21", "R_ROI_F_21", "R_Q_F_21")

Call RestoreFontsEtc(arGrp21)
End sub


Sub RestoreFontsEtc(arCht As Variant)
Dim chtobj As ChartObject
For Each chtobj In Sheet1.ChartObjects(arCht)
chtobj.Height = 300
chtobj.Width = 450
Next
End Sub



Bob Phillips[_6_]

Error after last object in the array when looping For...each
 
Gunnar,

Using your code I did a very quick test and I didn't get any problems. You
certainly don't have to tell VBA how many objects there are, that is the
point of For Each ... Next. You must have some other code that is affecting
it, or something about one of those chart objects.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Gunnar Johansson" wrote in message
...
Hi,
I have named chartobjects grouped as an array "arGrp21" and when I run the
"For each chtobj..." the loop works ok until the last, "R_Q_F_21", is

done.
After "R_Q_F_21" I get error with the line "Next" . Does I have to tell

how
many chartobjects there is? How? Else: What is the error with my code?

/Regards

Code Simplified:

Sub ArrayList
Dim arGrp21() As Variant

arGrp21 = Array("R_CF_FB_21", "R_ROI_FB_21", "R_Q_FB_21", "R_CF_FBA_21",
"R_ROI_FBA_21", "R_Q_FBA_21", "R_CF_F_21", "R_ROI_F_21", "R_Q_F_21")

Call RestoreFontsEtc(arGrp21)
End sub


Sub RestoreFontsEtc(arCht As Variant)
Dim chtobj As ChartObject
For Each chtobj In Sheet1.ChartObjects(arCht)
chtobj.Height = 300
chtobj.Width = 450
Next
End Sub





AA2e72E

Error after last object in the array when looping For...each
 
A chart can appear in a worksheet or in a chart sheet. So you have:

ActiveWorkbook.Charts ' in chart sheets
activeworkbook.activesheet.chartobjects ' charts in worksheets.

I suspect that one or more of your charts are in a chart sheet.
"Gunnar Johansson" wrote:

Hi,
I have named chartobjects grouped as an array "arGrp21" and when I run the
"For each chtobj..." the loop works ok until the last, "R_Q_F_21", is done.
After "R_Q_F_21" I get error with the line "Next" . Does I have to tell how
many chartobjects there is? How? Else: What is the error with my code?

/Regards

Code Simplified:

Sub ArrayList
Dim arGrp21() As Variant

arGrp21 = Array("R_CF_FB_21", "R_ROI_FB_21", "R_Q_FB_21", "R_CF_FBA_21",
"R_ROI_FBA_21", "R_Q_FBA_21", "R_CF_F_21", "R_ROI_F_21", "R_Q_F_21")

Call RestoreFontsEtc(arGrp21)
End sub


Sub RestoreFontsEtc(arCht As Variant)
Dim chtobj As ChartObject
For Each chtobj In Sheet1.ChartObjects(arCht)
chtobj.Height = 300
chtobj.Width = 450
Next
End Sub




Gunnar Johansson[_3_]

Error after last object in the array when looping For...each
 
Thanks for the help,

Now I know that the error isn't into this part of the code.
/regards

"Bob Phillips" skrev i meddelandet
...
Gunnar,

Using your code I did a very quick test and I didn't get any problems. You
certainly don't have to tell VBA how many objects there are, that is the
point of For Each ... Next. You must have some other code that is
affecting
it, or something about one of those chart objects.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Gunnar Johansson" wrote in message
...
Hi,
I have named chartobjects grouped as an array "arGrp21" and when I run
the
"For each chtobj..." the loop works ok until the last, "R_Q_F_21", is

done.
After "R_Q_F_21" I get error with the line "Next" . Does I have to tell

how
many chartobjects there is? How? Else: What is the error with my code?

/Regards

Code Simplified:

Sub ArrayList
Dim arGrp21() As Variant

arGrp21 = Array("R_CF_FB_21", "R_ROI_FB_21", "R_Q_FB_21", "R_CF_FBA_21",
"R_ROI_FBA_21", "R_Q_FBA_21", "R_CF_F_21", "R_ROI_F_21", "R_Q_F_21")

Call RestoreFontsEtc(arGrp21)
End sub


Sub RestoreFontsEtc(arCht As Variant)
Dim chtobj As ChartObject
For Each chtobj In Sheet1.ChartObjects(arCht)
chtobj.Height = 300
chtobj.Width = 450
Next
End Sub








All times are GMT +1. The time now is 01:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com