Create XLSX with different worksheets based upon table value

ino_mart

New Member
Joined
Oct 11, 2011
Messages
1
All

I am trying to create an Excel 2007 file with multiple worksheets via VBA-code in Access 2007. The name of a worksheet is based upon a value in a table. With code below, I get error message "run-time error 9: subscript out of range".

The first time the code goes into the while-block, it succeeds. But it fails the second time on line in red (see below). I expected this line would set focus to the worksheet I just created (but seems I got it wrong).


Code:
Private Sub CreateXLS
'create Excel object
Set oExcel = CreateObject("Excel.Application")
strFileName = "report.xlsx"
strSQL = "select distinct KPI from dbo.tblKPI"
Set rsKPI = New ADODB.Recordset
rsKPI.Open strSQL, cnCap, adOpenDynamic, adLockOptimistic
rsKPI.MoveFirst
 
IntHeaderRow = 6
intCounter = intHeaderRow + 1
intWorksheet = 1
While rsKPI.BOF = False And rsKPI.EOF = False
        Set oBook = oExcel.Workbooks.Add(intWorksheet)
        [COLOR=red]Set oSheet = oBook.Worksheets(intWorksheet)[/COLOR]
        oSheet.Name = rsKPI!kpi
 
        'Define titles in Headerrow
        oSheet.Range("A" & intHeaderRow).value = "Country: "
        '... publish some other titles
        strSQL = "select * from tblData where KPI='" & rsKPI!KPI & "'"
        set rsData = New ADODB.Recordset
        rsData.Open strSQL, cnCap, adOpenDynamic, adLockOptimistic
        while rsData.BOF = False and rsData.EOF = False
            '...export to Excel
             oSheet.Range("A" & intCounter).value = rsData!Country
             '... publish other fields of recordset rsData
             rsData.MoveNext
             intCounter = intCounter + 1
        wend
        intWorksheet = intWorksheet + 1
        intCounter = intHeaderRow + 1
        rsKPI.MoveNext
Wend
'Save file
oBook.SaveAs strFileName
oExcel.Quit
end sub
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Code:
        Set oBook = oExcel.Workbooks.Add([COLOR="Red"]intWorksheet[/COLOR])
        Set oSheet = oBook.Worksheets(intWorksheet)

try instead without a parameter in the add method:
Code:
        Set oBook = oExcel.Workbooks.Add
        Set oSheet = oBook.Worksheets(intWorksheet)

I'm not sure what you're trying to do with this intWorksheet variable actually ...
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,127
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top