Creating additional worksheets in an Excel Workbook from Access

JohnLee

Registered User.
Local time
Today, 03:55
Joined
Mar 8, 2007
Messages
692
Good afternoon folks,

I'm hoping someone can help me as I'm stuck on how to write code to create additional worksheets within an Active workbook.

Below is the code I've produced with the help of this forum and the code works fine, but I want to be able to add in additional worksheets to the active workbook add in the data and format it in the same way I have in my code below. I intend to add a further 28 worksheets within the active workbook. Any assistance/pointers would be most appreciated.

Code:
Dim ExcelFile As String
Dim ExcelWorksheet As String
Dim QUESDB As String
Dim QueryName As String
Dim objDB As Database
Dim MyDate As Date
Dim StdDGCount
MyDate = Date
StdDGCount = DCount("*", "qryStdDGQuesResponseStatsExportToExcel")
If StdDGCount > 0 Then
        ExcelFile = "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls"
        ExcelWorksheet = "ME " & Format(Date, "ddmmyy")
        QUESDB = "H:\John Lee\Questionnaires.mdb"
        QueryName = "qryStdDGQuesReponseStatsExportToExcel"
        Set objDB = OpenDatabase(QUESDB)
        If Dir(ExcelFile) <> "" Then Kill ExcelFile
        'Excute the creation of the Excel file
        objDB.Execute "Select*Into[Excel 8.0;Database=" & ExcelFile & "].[" & ExcelWorksheet & "] From " & "[" & QueryName & "]"
        objDB.Close
        Set objDB = Nothing
    
        Dim ObjExcel As Object
        Set ObjExcel = CreateObject("Excel.Application")
        ObjExcel.Visible = True
        
        ObjExcel.Workbooks.Open "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls"
    
        Set objsheet = ObjExcel.ActiveWorkbook.Worksheets(1)
        With objsheet
            .Rows("1:1").Font.Bold = True
            .Rows("1:1").Font.Underline = xlUnderlineStyleSingle
            .Rows("1:1").Select
            ObjExcel.Selection.Insert Shift:=xlDown
            ObjExcel.Range("A1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "Std D&G"
            ObjExcel.Range("B1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "Month Ending:"
            ObjExcel.Range("D1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = MyDate
            With ObjExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlBottom
            End With
            .Columns("A:CO").Select
            .Columns("A:CO").EntireColumn.AutoFit
            ObjExcel.Range("A2:CO31").Select
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A2:CO2").Select
            With ObjExcel.Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A2:CO32").Select
            With ObjExcel.Selection
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
            End With
            ObjExcel.Range("a32:CO32").Select
            With ObjExcel.Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A32").Select
            ActiveCell.FormulaR1C1 = "=Sum(R[-29]C:R[-1]C)"
            ObjExcel.Range("A32").Select
            ObjExcel.Selection.AutoFill Destination:=Range("A32:CN32"), Type:=xlFillDefault
            ObjExcel.Range("A32:CN32").Select
            ObjExcel.Range("A32:CN32").Select
            ObjExcel.Selection.Font.Bold = True
            ObjExcel.Selection.Font.ColorIndex = 3
        End With
    
        ObjExcel.ActiveWorkbook.Close True          'Close the Active Workbook
        ObjExcel.Quit                               'Close the Excel Application
End If

regards

John
 
You might try:
Code:
Set objsheet = ObjExcel.ActiveWorkbook.Worksheets.Add(, _
    ObjExcel.ActiveWorkbook.Sheets(ObjExcel.ActiveWorkbook.Sheets.Count))
 
Hi ByteMyzer,

Thanks for your reponse, I'll give that a go and let you know how I get on.

Regards

John
 
Hi ByteMyzer,

I tried your suggested code, but unfortunately my DB didn't like it, however with a littly playing around and using your code sample as a base, I've come up with this code below which works in that it creates a new worksheet and names it as required, however I now need......

Code:
ObjExcel.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "01_Q"

to work out how to input data from my query into the spreadsheet as per the following variable identifies the query in my DB for the data to be exported to the Excel spreadsheet:

QueryName = "qryStdDGQuesResponseStatsExportToExcel"

and the following line of code creates the spreadsheet:

Code:
objDB.Execute "Select*Into[Excel 8.0;Database=" & ExcelFile & "].[" & ExcelWorksheet & "] From " & "[" & QueryName & "]

but I'm not clear on how to extract or create the code that enables me to add in the data from my next query into my newly created 2nd worksheet. Clearly because the code above creates a new Excel file and worksheet and uses the QueryName variable to export the data.

If you can find some time to give me a pointer, it would be most appreciated.

Regards

John
 
Hi,

I've been trying a number of ways with the existing code to try and create additional tabs in the excel file and format them from access with no luck.

So I'm trying another approach in which I have been able to achieve getting all my desired data into the various individual tabs within one excel workbook. The problem I have is that I can only get formatting achieved on the first tab and I want to get formating to all of my tabs. Below is the code I have so far.......

Code:
Function ExportMonthlyStats()
On Error GoTo ExportMonthlyStats_Err
Dim StdQues01_Q
Dim StdQues02_Q
Dim StdQues03_Q
Dim StdQues04_Q
Dim StdQues05_Q
Dim StdQues06_Q
Dim StdQues07_Q
Dim StdQues10_Q
Dim StdQues11_Q
Dim StdQues12_Q
Dim StdQues13_Q
Dim StdQues14_Q
Dim StdQues17_Q
Dim StdQues18_Q
Dim StdQues19_Q
Dim StdQues20_Q
Dim StdQues21_Q
Dim StdQues23_Q
Dim StdQues24_Q
Dim StdQues28_Q
Dim StdQues29_Q
Dim StdQues30_Q
Dim StdQues31_Q
Dim StdQues32_Q
Dim StdQues36_Q
Dim StdQues37_Q
Dim StdQuesAR_Q
Dim StdQuesHE_Q
Dim StdQuesOverall
Dim MyDate As Date
StdQues01_Q = DCount("*", "01_Q")
StdQues02_Q = DCount("*", "02_Q")
StdQues03_Q = DCount("*", "03_Q")
StdQues04_Q = DCount("*", "04_Q")
StdQues05_Q = DCount("*", "05_Q")
StdQues06_Q = DCount("*", "06_Q")
StdQues07_Q = DCount("*", "07_Q")
StdQues10_Q = DCount("*", "10_Q")
StdQues11_Q = DCount("*", "11_Q")
StdQues12_Q = DCount("*", "12_Q")
StdQues13_Q = DCount("*", "13_Q")
StdQues14_Q = DCount("*", "14_Q")
StdQues17_Q = DCount("*", "17_Q")
StdQues18_Q = DCount("*", "18_Q")
StdQues19_Q = DCount("*", "19_Q")
StdQues20_Q = DCount("*", "20_Q")
StdQues21_Q = DCount("*", "21_Q")
StdQues23_Q = DCount("*", "23_Q")
StdQues24_Q = DCount("*", "24_Q")
StdQues28_Q = DCount("*", "28_Q")
StdQues29_Q = DCount("*", "29_Q")
StdQues30_Q = DCount("*", "30_Q")
StdQues31_Q = DCount("*", "31_Q")
StdQues32_Q = DCount("*", "32_Q")
StdQues36_Q = DCount("*", "36_Q")
StdQues37_Q = DCount("*", "37_Q")
StdQuesAR_Q = DCount("*", "AR_Q")
StdQuesHE_Q = DCount("*", "HE_Q")
StdQuesOverall = DCount("*", "StdDGQuesResponseMonthlyStats")
Dim ObjExcel As Object
        
MyDate = Date
        
If StdQuesOverall > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "StdDGQuesResponseMonthlyStats", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", True, ""
End If
If StdQues01_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "01_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues02_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "02_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues03_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "03_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues04_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "04_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues05_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "05_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues06_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "06_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues07_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "07_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues10_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "10_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues11_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "11_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues12_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "12_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues13_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "13_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues14_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "14_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues17_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "17_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues18_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "18_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues19_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "19_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues20_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "20_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues21_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "21_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues23_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "23_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues24_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "24_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues28_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "28_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues29_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "29_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues30_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "30_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues31_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "31_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues32_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "32_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues36_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "36_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQues37_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "37_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQuesAR_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "AR_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
If StdQuesHE_Q > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "HE_Q", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", False, ""
End If
 Set ObjExcel = CreateObject("Excel.Application")
    ObjExcel.Visible = True
    
    ObjExcel.Workbooks.Open "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls"
    
    Set objsheet = ObjExcel.ActiveWorkbook.Worksheets(1)
        With objsheet
            .Columns("A:CO").Select
            .Rows("1:1").Font.Bold = True
            .Rows("1:1").Font.Underline = xlUnderlineStyleSingle
            .Rows("1:1").Select
            ObjExcel.Selection.Insert Shift:=xlDown
            ObjExcel.Range("A1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "Std D&G"
            ObjExcel.Range("B1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "Month Ending:"
            ObjExcel.Range("D1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = MyDate
            With ObjExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlBottom
            End With
            .Columns("A:CO").Select
            .Columns("A:CO").EntireColumn.AutoFit
            ObjExcel.Range("A2:CO31").Select
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A2:CO2").Select
            With ObjExcel.Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A2:CO32").Select
            With ObjExcel.Selection
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
            End With
            ObjExcel.Range("a32:CO32").Select
            With ObjExcel.Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A32").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "=Sum(R[-29]C:R[-1]C)"
            ObjExcel.Range("A32").Select
            ObjExcel.Selection.AutoFill Destination:=Range("A32:CN32"), Type:=xlFillDefault
            ObjExcel.Range("A32:CN32").Select
            ObjExcel.Range("A32:CN32").Select
            ObjExcel.Selection.Font.Bold = True
            ObjExcel.Selection.Font.ColorIndex = 3
        End With
        
ObjExcel.ActiveWorkbook.Close True          'Close the Active Workbook
ObjExcel.Quit                               'Close the Excel Application
ExportMonthlyStats_Exit:
    Exit Function
ExportMonthlyStats_Err:
    MsgBox Error$
    Resume ExportMonthlyStats_Exit
End Function

Now if I put the following code......

Code:
Set ObjExcel = CreateObject("Excel.Application")
    ObjExcel.Visible = True
    
    ObjExcel.Workbooks.Open "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls"
    
    Set objsheet = ObjExcel.ActiveWorkbook.Worksheets(1)
        With objsheet
            .Columns("A:CO").Select
            .Rows("1:1").Font.Bold = True
            .Rows("1:1").Font.Underline = xlUnderlineStyleSingle
            .Rows("1:1").Select
            ObjExcel.Selection.Insert Shift:=xlDown
            ObjExcel.Range("A1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "Std D&G"
            ObjExcel.Range("B1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "Month Ending:"
            ObjExcel.Range("D1").Select
            ObjExcel.ActiveCell.FormulaR1C1 = MyDate
            With ObjExcel.Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlBottom
            End With
            .Columns("A:CO").Select
            .Columns("A:CO").EntireColumn.AutoFit
            ObjExcel.Range("A2:CO31").Select
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A2:CO2").Select
            With ObjExcel.Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A2:CO32").Select
            With ObjExcel.Selection
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
            End With
            ObjExcel.Range("a32:CO32").Select
            With ObjExcel.Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                .ColorIndex = xlAutomatic
            End With
            With ObjExcel.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlMedium
                ColorIndex = xlAutomatic
            End With
            ObjExcel.Range("A32").Select
            ObjExcel.ActiveCell.FormulaR1C1 = "=Sum(R[-29]C:R[-1]C)"
            ObjExcel.Range("A32").Select
            ObjExcel.Selection.AutoFill Destination:=Range("A32:CN32"), Type:=xlFillDefault
            ObjExcel.Range("A32:CN32").Select
            ObjExcel.Range("A32:CN32").Select
            ObjExcel.Selection.Font.Bold = True
            ObjExcel.Selection.Font.ColorIndex = 3
        End With


between this line of code 'HERE:

Code:
If StdQuesOverall > 0 Then
    DoCmd.TransferSpreadsheet acExport, 8, "StdDGQuesResponseMonthlyStats", "M:\Customer Satisfaction\Standard D & G\Monthly Reports\ME" & "_" & Format(Date, "ddmmyy") & ".xls", True, ""
    
    [COLOR=red]'HERE[/COLOR]
End If

the code stops at this line of code:

Code:
ObjExcel.Range("A32").Select

and I get the following message: "object variable or with block variable not set"

But if I have the code where it is shown in the first code block example above it is fine, but then when I to add after that block of code the same block of code only this time looking at worksheet 2, i.e. I change this line of code from......

Code:
Set objsheet = ObjExcel.ActiveWorkbook.Worksheets(1)

To this

Code:
Set objsheet = ObjExcel.ActiveWorkbook.Worksheets(2)

and this line of code from......

Code:
ObjExcel.ActiveCell.FormulaR1C1 = "Std D&G"

To this line of code:

Code:
ObjExcel.ActiveCell.FormulaR1C1 = "Std D&G 01_Q"

and run my code, it creates the workbook and formats up to the line of code

Code:
ObjExcel.Range("A32").Select

and then I see the following error message:

"Method 'Range' of object '_Global' failed"

so I'm somewhat stuck as to how I can apply formatting to the rest of my tabs.

I hope I have explained what it is I am trying to achieve clearly enough, please let me know if you require any more information.

Any help offered would be gratefully received.

Regards

John
 
Hi,

Further to my last post, attached is the resulting spreadsheet with all the various tabs, I have been able to get the first tab formatted, I'm really struggleing to get the remainder of the tabs formatted, in my last post the first code block showed the code for formatting which came after the spreadsheet had been created with all the required tabs data, and this codes works but I can only get it to work for the first tab.

Any suggestions or pointers would be most appreciated.

Regards

John
 

Attachments

Users who are viewing this thread

Back
Top Bottom