Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Object variable or With Block variable not set

Hi all,

I keep getting this run-time error '91' message when I run this macro. It
works fine for the first time but not subsequently. I believe it has to do
with the bug on Selection. I'm trying to create a chart based on a dynamic
range of data that's imported from MS Access 2003. Therefore I need to select
the current range that's present on my ActiveSheet. Any idea how I can solve
this problem? Any help is appreciated. Thanks.

The following is part of my code:

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer

Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"

With xlsheet
xl.Range("A2").CopyFromRecordset MyRecordset
End With

' Enumerating through to add column headings
C = 1
For I = 0 To MyRecordset.Fields.Count - 1
xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
C = C + 1
Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
dataRange = xl.Selection.Address

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
ActiveWorkbook.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(dataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Object variable or With Block variable not set

Sometimes you need to add the following. Don't ask why, just try it.

Charts.Add
AvtiveChart.Activate '<= ADD
ActiveChart.ChartType = xlColumnClustered


"acccessaccess2003" wrote:

Hi all,

I keep getting this run-time error '91' message when I run this macro. It
works fine for the first time but not subsequently. I believe it has to do
with the bug on Selection. I'm trying to create a chart based on a dynamic
range of data that's imported from MS Access 2003. Therefore I need to select
the current range that's present on my ActiveSheet. Any idea how I can solve
this problem? Any help is appreciated. Thanks.

The following is part of my code:

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer

Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"

With xlsheet
xl.Range("A2").CopyFromRecordset MyRecordset
End With

' Enumerating through to add column headings
C = 1
For I = 0 To MyRecordset.Fields.Count - 1
xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
C = C + 1
Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
dataRange = xl.Selection.Address

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
ActiveWorkbook.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(dataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Object variable or With Block variable not set

Hey Joel,

Thanks for your reply. I still get back the same error despite adding the
line of code you've instructed. Any other remedies?

"Joel" wrote:

Sometimes you need to add the following. Don't ask why, just try it.

Charts.Add
AvtiveChart.Activate '<= ADD
ActiveChart.ChartType = xlColumnClustered


"acccessaccess2003" wrote:

Hi all,

I keep getting this run-time error '91' message when I run this macro. It
works fine for the first time but not subsequently. I believe it has to do
with the bug on Selection. I'm trying to create a chart based on a dynamic
range of data that's imported from MS Access 2003. Therefore I need to select
the current range that's present on my ActiveSheet. Any idea how I can solve
this problem? Any help is appreciated. Thanks.

The following is part of my code:

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer

Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"

With xlsheet
xl.Range("A2").CopyFromRecordset MyRecordset
End With

' Enumerating through to add column headings
C = 1
For I = 0 To MyRecordset.Fields.Count - 1
xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
C = C + 1
Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
dataRange = xl.Selection.Address

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
ActiveWorkbook.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(dataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Object variable or With Block variable not set

Which line gives the error?

"acccessaccess2003" wrote:

Hey Joel,

Thanks for your reply. I still get back the same error despite adding the
line of code you've instructed. Any other remedies?

"Joel" wrote:

Sometimes you need to add the following. Don't ask why, just try it.

Charts.Add
AvtiveChart.Activate '<= ADD
ActiveChart.ChartType = xlColumnClustered


"acccessaccess2003" wrote:

Hi all,

I keep getting this run-time error '91' message when I run this macro. It
works fine for the first time but not subsequently. I believe it has to do
with the bug on Selection. I'm trying to create a chart based on a dynamic
range of data that's imported from MS Access 2003. Therefore I need to select
the current range that's present on my ActiveSheet. Any idea how I can solve
this problem? Any help is appreciated. Thanks.

The following is part of my code:

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer

Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"

With xlsheet
xl.Range("A2").CopyFromRecordset MyRecordset
End With

' Enumerating through to add column headings
C = 1
For I = 0 To MyRecordset.Fields.Count - 1
xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
C = C + 1
Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
dataRange = xl.Selection.Address

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
ActiveWorkbook.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(dataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Object variable or With Block variable not set

I copied that particular function into a module and run it. On the Excel
ActiveSheet it doesn't show the intended chart so I checked back to VBA IDE
and it shows a dialog box that reads the run-time error 91 msg. I clicked on
'Debug' and the yellow-highlighted error code is as follows:

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"

right after dataRange = xl.Selection.Address.

"Joel" wrote:

Which line gives the error?

"acccessaccess2003" wrote:

Hey Joel,

Thanks for your reply. I still get back the same error despite adding the
line of code you've instructed. Any other remedies?

"Joel" wrote:

Sometimes you need to add the following. Don't ask why, just try it.

Charts.Add
AvtiveChart.Activate '<= ADD
ActiveChart.ChartType = xlColumnClustered


"acccessaccess2003" wrote:

Hi all,

I keep getting this run-time error '91' message when I run this macro. It
works fine for the first time but not subsequently. I believe it has to do
with the bug on Selection. I'm trying to create a chart based on a dynamic
range of data that's imported from MS Access 2003. Therefore I need to select
the current range that's present on my ActiveSheet. Any idea how I can solve
this problem? Any help is appreciated. Thanks.

The following is part of my code:

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer

Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"

With xlsheet
xl.Range("A2").CopyFromRecordset MyRecordset
End With

' Enumerating through to add column headings
C = 1
For I = 0 To MyRecordset.Fields.Count - 1
xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
C = C + 1
Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
dataRange = xl.Selection.Address

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
ActiveWorkbook.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(dataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Object variable or With Block variable not set

I have two sub below test and test2. I did not have a record set so I
comment out some of the code and got test to run over and over again without
error. I think the problem is with activeworkbook. so I created test2 (did
not try) with referenciing the new workbook using xlwkbk. See code below

Sub test()

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
'Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer
Set old = ThisWorkbook
' Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

' MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"


xlsheet.Range("A1:C8") = 3
' With xlsheet
' xl.Range("A2").CopyFromRecordset MyRecordset
' End With

' Enumerating through to add column headings
C = 1
' For I = 0 To MyRecordset.Fields.Count - 1
' xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
' C = C + 1
' Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
DataRange = xl.Selection.Address

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
ActiveWorkbook.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(DataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"


End Sub

Sub test2()

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
'Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer
Set old = ThisWorkbook
Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"


xlsheet.Range("A1:C8") = 3
With xlsheet
xl.Range("A2").CopyFromRecordset MyRecordset
End With

' Enumerating through to add column headings
C = 1
For I = 0 To MyRecordset.Fields.Count - 1
xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
' C = C + 1
Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
DataRange = xl.Selection.Address

With xlwkbk
.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

.Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(DataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"
End With

End Sub



"acccessaccess2003" wrote:

I copied that particular function into a module and run it. On the Excel
ActiveSheet it doesn't show the intended chart so I checked back to VBA IDE
and it shows a dialog box that reads the run-time error 91 msg. I clicked on
'Debug' and the yellow-highlighted error code is as follows:

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"

right after dataRange = xl.Selection.Address.

"Joel" wrote:

Which line gives the error?

"acccessaccess2003" wrote:

Hey Joel,

Thanks for your reply. I still get back the same error despite adding the
line of code you've instructed. Any other remedies?

"Joel" wrote:

Sometimes you need to add the following. Don't ask why, just try it.

Charts.Add
AvtiveChart.Activate '<= ADD
ActiveChart.ChartType = xlColumnClustered


"acccessaccess2003" wrote:

Hi all,

I keep getting this run-time error '91' message when I run this macro. It
works fine for the first time but not subsequently. I believe it has to do
with the bug on Selection. I'm trying to create a chart based on a dynamic
range of data that's imported from MS Access 2003. Therefore I need to select
the current range that's present on my ActiveSheet. Any idea how I can solve
this problem? Any help is appreciated. Thanks.

The following is part of my code:

Dim xl As Excel.Application
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim MyRecordset As ADODB.Recordset
Dim rowCount As Integer
Dim colCount As Integer
Dim I As Integer
Dim C As Integer

Set MyRecordset = New ADODB.Recordset
Set xl = New Excel.Application
Set xlwkbk = xl.Workbooks.Add

xl.Visible = True

MyRecordset.Open "tblName", CurrentProject.Connection, adOpenStatic

Set xlsheet = xlwkbk.Worksheets.Add
xlsheet.Name = "Report Name"

With xlsheet
xl.Range("A2").CopyFromRecordset MyRecordset
End With

' Enumerating through to add column headings
C = 1
For I = 0 To MyRecordset.Fields.Count - 1
xl.ActiveSheet.Cells(1, C).Value = MyRecordset.Fields(I).Name
C = C + 1
Next I

rowCount = xlsheet.UsedRange.Rows.Count
colCount = xlsheet.UsedRange.Columns.Count

xl.Selection.CurrentRegion.Select
dataRange = xl.Selection.Address

ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C1,1,0,COUNTA('Report Name'!C1)-1)"
ActiveWorkbook.Names.Add Name:="Target", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C2,1,0,COUNTA('Report Name'!C2)-1)"
ActiveWorkbook.Names.Add Name:="Week", RefersToR1C1:= _
"=OFFSET('Report Name'!R1C3,1,0,COUNTA('Report Name'!C3)-1)"

Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Report
Name").Range(dataRange), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "='Report Name'!Week"
ActiveChart.SeriesCollection(2).XValues = "='Report Name'!Week"
ActiveChart.Location Whe=xlLocationAsObject, Name:="Report Name"

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Number: 91 object variable or With block Variable not set Hifni Excel Programming 1 January 9th 08 11:56 AM
Need help with Error 'object variable or with block variable not set' Francois via OfficeKB.com Excel Programming 5 April 30th 07 03:05 PM
Run-time Error'91: Object variable or With block variable not set DynamiteSkippy Excel Programming 4 September 26th 05 07:47 AM
Run-time error '91': "Object variable or With block variable not set Mike[_92_] Excel Programming 2 December 30th 04 10:59 AM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"