PC Review


Reply
Thread Tools Rate Thread

Code stops for no apparent reason

 
 
=?Utf-8?B?UmVuw6k=?=
Guest
Posts: n/a
 
      29th Dec 2006
Hello,

I wrote some code, that creates multiple charts (more than 10 or even 20).
The code works fine, but after a short while, it stops for no apparent
reason, while VBE indicates that the code is still running. If I want to stop
it, Excel quits.
I wonder what is causing this. Does someone have the answer? I has probably
to do with the loop, I guess.
See below for the code.

greetings
René

Sub CreateDiskChart()

Dim Bereik
Dim strSheetName As String, strSheetName2 As String, strChartTitle As
String, strWorkBook As String
Dim intRow As Integer, intCharts As Integer

strSheetName = ActiveSheet.Name
Sheets.Add
strSheetName2 = ActiveSheet.Name
strWorkBook = ActiveWorkbook.Name
intCharts =
Application.WorksheetFunction.CountIf(Sheets(strSheetName).Range("A:A"),
"customer")
Sheets(strSheetName).Select
Cells(1).Select

For n = 1 To intCharts
intRow = ActiveCell.Row
Set Bereik = Range(Cells(intRow, 4), Cells(Cells(intRow,
1).CurrentRegion.Rows.Count + intRow - 1, 7))
Range(Cells(intRow, 4), Cells(Cells(intRow,
1).CurrentRegion.Rows.Count + intRow - 1, 7)).Select
strChartTitle = ActiveCell.Item(2, 0).Value & " (" &
MonthName(ActiveCell.Item(2, -1).Value, False) & ")"

Charts.Add
ActiveChart.ChartType = xlColumnClustered

ActiveChart.SetSourceData Source:=Bereik, PlotBy:= _
xlColumns
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
ActiveChart.Location WHERE:=xlLocationAsObject, Name:=strSheetName2


With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = strChartTitle
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Disk"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text =
"Percentage used"
.SeriesCollection(3).Select
.ChartGroups(1).SeriesCollection(3).PlotOrder = 1
End With
z = ActiveSheet.ChartObjects.Count
ActiveSheet.Shapes(z).IncrementLeft 50 + 10 * n
ActiveSheet.Shapes(z).IncrementTop 50 + 10 * n


Workbooks(strWorkBook).Activate
Sheets(strSheetName).Select
ActiveCell.End(xlDown).Select
ActiveCell.End(xlDown).Select

Sheets(strSheetName).Select
Next n
Range("A1").Select
End Sub

 
Reply With Quote
 
 
 
 
gimme_this_gimme_that@yahoo.com
Guest
Posts: n/a
 
      29th Dec 2006
Your code could be iterating through an infinite loop because of the
inCharts assignment might result in questionable values.

In anycase the clip below should be rewritten so there is a single
assignment to the Worksheet from a two dimensional array. This code is
so inefficent that it's not even funny.

For n = 1 To intCharts
intRow = ActiveCell.Row
Set Bereik = Range(Cells(intRow, 4), Cells(Cells(intRow,
1).CurrentRegion.Rows.Count + intRow - 1, 7))
Range(Cells(intRow, 4), Cells(Cells(intRow,
1).CurrentRegion.Rows.Count + intRow - 1, 7)).Select
strChartTitle = ActiveCell.Item(2, 0).Value & " (" &
MonthName(ActiveCell.Item(2, -1).Value, False) & ")"



René wrote:
> Hello,
>
> I wrote some code, that creates multiple charts (more than 10 or even 20).
> The code works fine, but after a short while, it stops for no apparent
> reason, while VBE indicates that the code is still running. If I want to stop
> it, Excel quits.
> I wonder what is causing this. Does someone have the answer? I has probably
> to do with the loop, I guess.
> See below for the code.
>
> greetings
> René
>
> Sub CreateDiskChart()
>
> Dim Bereik
> Dim strSheetName As String, strSheetName2 As String, strChartTitle As
> String, strWorkBook As String
> Dim intRow As Integer, intCharts As Integer
>
> strSheetName = ActiveSheet.Name
> Sheets.Add
> strSheetName2 = ActiveSheet.Name
> strWorkBook = ActiveWorkbook.Name
> intCharts =
> Application.WorksheetFunction.CountIf(Sheets(strSheetName).Range("A:A"),
> "customer")
> Sheets(strSheetName).Select
> Cells(1).Select
>
> For n = 1 To intCharts
> intRow = ActiveCell.Row
> Set Bereik = Range(Cells(intRow, 4), Cells(Cells(intRow,
> 1).CurrentRegion.Rows.Count + intRow - 1, 7))
> Range(Cells(intRow, 4), Cells(Cells(intRow,
> 1).CurrentRegion.Rows.Count + intRow - 1, 7)).Select
> strChartTitle = ActiveCell.Item(2, 0).Value & " (" &
> MonthName(ActiveCell.Item(2, -1).Value, False) & ")"
>
> Charts.Add
> ActiveChart.ChartType = xlColumnClustered
>
> ActiveChart.SetSourceData Source:=Bereik, PlotBy:= _
> xlColumns
> ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
> ActiveChart.Location WHERE:=xlLocationAsObject, Name:=strSheetName2
>
>
> With ActiveChart
> .HasTitle = True
> .ChartTitle.Characters.Text = strChartTitle
> .Axes(xlCategory, xlPrimary).HasTitle = True
> .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Disk"
> .Axes(xlValue, xlPrimary).HasTitle = True
> .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text =
> "Percentage used"
> .SeriesCollection(3).Select
> .ChartGroups(1).SeriesCollection(3).PlotOrder = 1
> End With
> z = ActiveSheet.ChartObjects.Count
> ActiveSheet.Shapes(z).IncrementLeft 50 + 10 * n
> ActiveSheet.Shapes(z).IncrementTop 50 + 10 * n
>
>
> Workbooks(strWorkBook).Activate
> Sheets(strSheetName).Select
> ActiveCell.End(xlDown).Select
> ActiveCell.End(xlDown).Select
>
> Sheets(strSheetName).Select
> Next n
> Range("A1").Select
> End Sub


 
Reply With Quote
 
=?Utf-8?B?UmVuw6k=?=
Guest
Posts: n/a
 
      30th Dec 2006
Hi Jim

I already solved the problem by stepping through the code with F8. The
strange thing is that it stops at a certain lign, but that no error is given.
Thanks for the effort.

René

"Jim Jackson" wrote:

> Do you mean "ctrl/Break" causes a complete stop? I would place several
> "Stop"s in the code and run it, assessing the situation at each stop. It
> could be that, if you add "Do....Loop until (criteria is met)" the problem
> can be overcome.
>
> If you can provide more detail, please do so and more ideas can be offered.
> --
> Best wishes,
>
> Jim
>
>
> "René" wrote:
>
> > Hello,
> >
> > I wrote some code, that creates multiple charts (more than 10 or even 20).
> > The code works fine, but after a short while, it stops for no apparent
> > reason, while VBE indicates that the code is still running. If I want to stop
> > it, Excel quits.
> > I wonder what is causing this. Does someone have the answer? I has probably
> > to do with the loop, I guess.
> > See below for the code.
> >
> > greetings
> > René
> >
> > Sub CreateDiskChart()
> >
> > Dim Bereik
> > Dim strSheetName As String, strSheetName2 As String, strChartTitle As
> > String, strWorkBook As String
> > Dim intRow As Integer, intCharts As Integer
> >
> > strSheetName = ActiveSheet.Name
> > Sheets.Add
> > strSheetName2 = ActiveSheet.Name
> > strWorkBook = ActiveWorkbook.Name
> > intCharts =
> > Application.WorksheetFunction.CountIf(Sheets(strSheetName).Range("A:A"),
> > "customer")
> > Sheets(strSheetName).Select
> > Cells(1).Select
> >
> > For n = 1 To intCharts
> > intRow = ActiveCell.Row
> > Set Bereik = Range(Cells(intRow, 4), Cells(Cells(intRow,
> > 1).CurrentRegion.Rows.Count + intRow - 1, 7))
> > Range(Cells(intRow, 4), Cells(Cells(intRow,
> > 1).CurrentRegion.Rows.Count + intRow - 1, 7)).Select
> > strChartTitle = ActiveCell.Item(2, 0).Value & " (" &
> > MonthName(ActiveCell.Item(2, -1).Value, False) & ")"
> >
> > Charts.Add
> > ActiveChart.ChartType = xlColumnClustered
> >
> > ActiveChart.SetSourceData Source:=Bereik, PlotBy:= _
> > xlColumns
> > ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
> > ActiveChart.Location WHERE:=xlLocationAsObject, Name:=strSheetName2
> >
> >
> > With ActiveChart
> > .HasTitle = True
> > .ChartTitle.Characters.Text = strChartTitle
> > .Axes(xlCategory, xlPrimary).HasTitle = True
> > .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Disk"
> > .Axes(xlValue, xlPrimary).HasTitle = True
> > .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text =
> > "Percentage used"
> > .SeriesCollection(3).Select
> > .ChartGroups(1).SeriesCollection(3).PlotOrder = 1
> > End With
> > z = ActiveSheet.ChartObjects.Count
> > ActiveSheet.Shapes(z).IncrementLeft 50 + 10 * n
> > ActiveSheet.Shapes(z).IncrementTop 50 + 10 * n
> >
> >
> > Workbooks(strWorkBook).Activate
> > Sheets(strSheetName).Select
> > ActiveCell.End(xlDown).Select
> > ActiveCell.End(xlDown).Select
> >
> > Sheets(strSheetName).Select
> > Next n
> > Range("A1").Select
> > End Sub
> >

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      30th Dec 2006
It is quite possible that VBA hasn't cleaned up its internal storage of the
code. This can cause very odd errors, including the one you describe. The
solution is to Export the VBA modules to text files, Remove the modules from
the project, and Import the text files back into the project.

Rob Bovey has an excellent add-in that automates all of this. See
http://www.appspro.com/Utilities/CodeCleaner.htm for details. Rob's Code
Cleaner is a "must have" for serious Excel developers.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"René" <(E-Mail Removed)> wrote in message
news:1448A309-D607-430E-8B36-(E-Mail Removed)...
> Hi Jim
>
> I already solved the problem by stepping through the code with F8. The
> strange thing is that it stops at a certain lign, but that no error is
> given.
> Thanks for the effort.
>
> René
>
> "Jim Jackson" wrote:
>
>> Do you mean "ctrl/Break" causes a complete stop? I would place several
>> "Stop"s in the code and run it, assessing the situation at each stop. It
>> could be that, if you add "Do....Loop until (criteria is met)" the
>> problem
>> can be overcome.
>>
>> If you can provide more detail, please do so and more ideas can be
>> offered.
>> --
>> Best wishes,
>>
>> Jim
>>
>>
>> "René" wrote:
>>
>> > Hello,
>> >
>> > I wrote some code, that creates multiple charts (more than 10 or even
>> > 20).
>> > The code works fine, but after a short while, it stops for no apparent
>> > reason, while VBE indicates that the code is still running. If I want
>> > to stop
>> > it, Excel quits.
>> > I wonder what is causing this. Does someone have the answer? I has
>> > probably
>> > to do with the loop, I guess.
>> > See below for the code.
>> >
>> > greetings
>> > René
>> >
>> > Sub CreateDiskChart()
>> >
>> > Dim Bereik
>> > Dim strSheetName As String, strSheetName2 As String, strChartTitle
>> > As
>> > String, strWorkBook As String
>> > Dim intRow As Integer, intCharts As Integer
>> >
>> > strSheetName = ActiveSheet.Name
>> > Sheets.Add
>> > strSheetName2 = ActiveSheet.Name
>> > strWorkBook = ActiveWorkbook.Name
>> > intCharts =
>> > Application.WorksheetFunction.CountIf(Sheets(strSheetName).Range("A:A"),
>> > "customer")
>> > Sheets(strSheetName).Select
>> > Cells(1).Select
>> >
>> > For n = 1 To intCharts
>> > intRow = ActiveCell.Row
>> > Set Bereik = Range(Cells(intRow, 4), Cells(Cells(intRow,
>> > 1).CurrentRegion.Rows.Count + intRow - 1, 7))
>> > Range(Cells(intRow, 4), Cells(Cells(intRow,
>> > 1).CurrentRegion.Rows.Count + intRow - 1, 7)).Select
>> > strChartTitle = ActiveCell.Item(2, 0).Value & " (" &
>> > MonthName(ActiveCell.Item(2, -1).Value, False) & ")"
>> >
>> > Charts.Add
>> > ActiveChart.ChartType = xlColumnClustered
>> >
>> > ActiveChart.SetSourceData Source:=Bereik, PlotBy:= _
>> > xlColumns
>> > ActiveChart.Axes(xlCategory, xlPrimary).CategoryType =
>> > xlCategoryScale
>> > ActiveChart.Location WHERE:=xlLocationAsObject,
>> > Name:=strSheetName2
>> >
>> >
>> > With ActiveChart
>> > .HasTitle = True
>> > .ChartTitle.Characters.Text = strChartTitle
>> > .Axes(xlCategory, xlPrimary).HasTitle = True
>> > .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text =
>> > "Disk"
>> > .Axes(xlValue, xlPrimary).HasTitle = True
>> > .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text =
>> > "Percentage used"
>> > .SeriesCollection(3).Select
>> > .ChartGroups(1).SeriesCollection(3).PlotOrder = 1
>> > End With
>> > z = ActiveSheet.ChartObjects.Count
>> > ActiveSheet.Shapes(z).IncrementLeft 50 + 10 * n
>> > ActiveSheet.Shapes(z).IncrementTop 50 + 10 * n
>> >
>> >
>> > Workbooks(strWorkBook).Activate
>> > Sheets(strSheetName).Select
>> > ActiveCell.End(xlDown).Select
>> > ActiveCell.End(xlDown).Select
>> >
>> > Sheets(strSheetName).Select
>> > Next n
>> > Range("A1").Select
>> > End Sub
>> >



 
Reply With Quote
 
=?Utf-8?B?UmVuw6k=?=
Guest
Posts: n/a
 
      30th Dec 2006
Thanks Chip,

I'll give this a try

greetings
René

"Chip Pearson" wrote:

> It is quite possible that VBA hasn't cleaned up its internal storage of the
> code. This can cause very odd errors, including the one you describe. The
> solution is to Export the VBA modules to text files, Remove the modules from
> the project, and Import the text files back into the project.
>
> Rob Bovey has an excellent add-in that automates all of this. See
> http://www.appspro.com/Utilities/CodeCleaner.htm for details. Rob's Code
> Cleaner is a "must have" for serious Excel developers.
>
>
> --
> Cordially,
> Chip Pearson
> Microsoft MVP - Excel
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email address is on the web site)
>
>
>
> "René" <(E-Mail Removed)> wrote in message
> news:1448A309-D607-430E-8B36-(E-Mail Removed)...
> > Hi Jim
> >
> > I already solved the problem by stepping through the code with F8. The
> > strange thing is that it stops at a certain lign, but that no error is
> > given.
> > Thanks for the effort.
> >
> > René
> >
> > "Jim Jackson" wrote:
> >
> >> Do you mean "ctrl/Break" causes a complete stop? I would place several
> >> "Stop"s in the code and run it, assessing the situation at each stop. It
> >> could be that, if you add "Do....Loop until (criteria is met)" the
> >> problem
> >> can be overcome.
> >>
> >> If you can provide more detail, please do so and more ideas can be
> >> offered.
> >> --
> >> Best wishes,
> >>
> >> Jim
> >>
> >>
> >> "René" wrote:
> >>
> >> > Hello,
> >> >
> >> > I wrote some code, that creates multiple charts (more than 10 or even
> >> > 20).
> >> > The code works fine, but after a short while, it stops for no apparent
> >> > reason, while VBE indicates that the code is still running. If I want
> >> > to stop
> >> > it, Excel quits.
> >> > I wonder what is causing this. Does someone have the answer? I has
> >> > probably
> >> > to do with the loop, I guess.
> >> > See below for the code.
> >> >
> >> > greetings
> >> > René
> >> >
> >> > Sub CreateDiskChart()
> >> >
> >> > Dim Bereik
> >> > Dim strSheetName As String, strSheetName2 As String, strChartTitle
> >> > As
> >> > String, strWorkBook As String
> >> > Dim intRow As Integer, intCharts As Integer
> >> >
> >> > strSheetName = ActiveSheet.Name
> >> > Sheets.Add
> >> > strSheetName2 = ActiveSheet.Name
> >> > strWorkBook = ActiveWorkbook.Name
> >> > intCharts =
> >> > Application.WorksheetFunction.CountIf(Sheets(strSheetName).Range("A:A"),
> >> > "customer")
> >> > Sheets(strSheetName).Select
> >> > Cells(1).Select
> >> >
> >> > For n = 1 To intCharts
> >> > intRow = ActiveCell.Row
> >> > Set Bereik = Range(Cells(intRow, 4), Cells(Cells(intRow,
> >> > 1).CurrentRegion.Rows.Count + intRow - 1, 7))
> >> > Range(Cells(intRow, 4), Cells(Cells(intRow,
> >> > 1).CurrentRegion.Rows.Count + intRow - 1, 7)).Select
> >> > strChartTitle = ActiveCell.Item(2, 0).Value & " (" &
> >> > MonthName(ActiveCell.Item(2, -1).Value, False) & ")"
> >> >
> >> > Charts.Add
> >> > ActiveChart.ChartType = xlColumnClustered
> >> >
> >> > ActiveChart.SetSourceData Source:=Bereik, PlotBy:= _
> >> > xlColumns
> >> > ActiveChart.Axes(xlCategory, xlPrimary).CategoryType =
> >> > xlCategoryScale
> >> > ActiveChart.Location WHERE:=xlLocationAsObject,
> >> > Name:=strSheetName2
> >> >
> >> >
> >> > With ActiveChart
> >> > .HasTitle = True
> >> > .ChartTitle.Characters.Text = strChartTitle
> >> > .Axes(xlCategory, xlPrimary).HasTitle = True
> >> > .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text =
> >> > "Disk"
> >> > .Axes(xlValue, xlPrimary).HasTitle = True
> >> > .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text =
> >> > "Percentage used"
> >> > .SeriesCollection(3).Select
> >> > .ChartGroups(1).SeriesCollection(3).PlotOrder = 1
> >> > End With
> >> > z = ActiveSheet.ChartObjects.Count
> >> > ActiveSheet.Shapes(z).IncrementLeft 50 + 10 * n
> >> > ActiveSheet.Shapes(z).IncrementTop 50 + 10 * n
> >> >
> >> >
> >> > Workbooks(strWorkBook).Activate
> >> > Sheets(strSheetName).Select
> >> > ActiveCell.End(xlDown).Select
> >> > ActiveCell.End(xlDown).Select
> >> >
> >> > Sheets(strSheetName).Select
> >> > Next n
> >> > Range("A1").Select
> >> > End Sub
> >> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recordsetclone.Recordcount stops working for no apparent reason Julia Boswell Microsoft Access Form Coding 1 2nd Feb 2006 02:15 PM
Database suddenly stops allowing updates ! For no apparent Reason. =?Utf-8?B?U2Nhc2FsZXR0bw==?= Microsoft Access Queries 0 31st May 2005 09:18 AM
App switches to code-view breakpoint for no apparent reason jfro Microsoft Access 2 8th Mar 2005 07:21 PM
Form goes into code for no apparent reason William Oliveri Microsoft Access Forms 9 14th Jan 2005 06:39 PM
OL 2000 stops w/no apparent reason. Scott Richardson Microsoft Outlook 0 20th Aug 2004 05:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:26 AM.