I am creating a form that selects data based on a user selected model type, flange type, and a range of line sizes. This hasn't been a problem, I am able to get the correct report from the form thus far.
My issue is the button I am using to export the report to excel. I want the button to save the file with a filename based on the parameters selected in the form, then open the excel file, delete empty columns, move the table a bit (just so the borders are visible), and then add the lines to make it look like a nice table. I am at the point where this works most of the time when I click the button. It seems to be not working properly whenever excel is already open, especially if there is an excel file that is already open which was created by this button. What really gets me is that it'll give a "Method 'Range' of object '_Global' " error sometimes but if I close the excel file and try again it will work no problem.
Keep in mind that I am a complete novice when it comes to Access and VBA in general - everything you see here I have taught myself in the last week. So feel free to treat me like a child, I am learning each step of the way.
I have omitted all the code concerning the option lists on the form. This is just the button.
My issue is the button I am using to export the report to excel. I want the button to save the file with a filename based on the parameters selected in the form, then open the excel file, delete empty columns, move the table a bit (just so the borders are visible), and then add the lines to make it look like a nice table. I am at the point where this works most of the time when I click the button. It seems to be not working properly whenever excel is already open, especially if there is an excel file that is already open which was created by this button. What really gets me is that it'll give a "Method 'Range' of object '_Global' " error sometimes but if I close the excel file and try again it will work no problem.
Keep in mind that I am a complete novice when it comes to Access and VBA in general - everything you see here I have taught myself in the last week. So feel free to treat me like a child, I am learning each step of the way.
I have omitted all the code concerning the option lists on the form. This is just the button.
Code:
Option Compare Database
Option Explicit
Private objExcel As Excel.Application
Private xlwb As Excel.Workbook
Private xlws As Excel.Worksheet
Private Sub CreateTable_Click()
Dim filename As String
Dim lastrow As Long
Dim lastcolumn As Long
Dim i As Long
Dim flange As String
Select Case FlangeType.Value
Case 1
flange = "SO"
Case 2
flange = "WN"
Case 3
flange = "WA"
End Select
'set filename based on filter of table and save as excel sheet
filename = "Type1drawing_" & DrawingType.Value & "_" & flange & "flange_" & minsize.Value & "_to_" & maxsize.Value & ".xls"
DoCmd.OutputTo acOutputReport, "rptType1Drawing", "Excel97-Excel2003Workbook(*.xls)", filename, False, "", 0, acExportQualityPrint
'set an excel object and open the saved file to format correctly
Set objExcel = New Excel.Application
objExcel.Visible = True
Set xlwb = objExcel.Workbooks.Open(filename)
Set xlws = objExcel.Worksheets("Type 1 Drawing Maker")
objExcel.ScreenUpdating = False
objExcel.Calculation = xlCalculationManual
xlws.Activate
With xlws
lastrow = Range("A1").End(xlDown).Row
lastcolumn = Range("A1").End(xlToRight).Column
i = lastcolumn
'delete empty columns (non-available liners)
Do Until i = 0
If WorksheetFunction.CountA(Range(Cells(2, i), Cells(lastrow, i))) = 0 Then 'use row 2 so the headers aren't counted
Columns(i).Delete
End If
i = i - 1
Loop
'move for better view and then format
Range("A1", Cells(lastrow, lastcolumn)).Cut
Range("b2").Select
ActiveSheet.Paste
'update the last column and row
lastcolumn = Range("b2").End(xlToRight).Column
lastrow = Range("b2").End(xlDown).Row
Range("b2", Cells(lastrow, lastcolumn)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection
.RowHeight = 14
.VerticalAlignment = xlCenter
End With
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
Columns.AutoFit
Cells(1, 1).Select
End With
objExcel.ScreenUpdating = True
objExcel.Calculation = xlCalculationAutomatic
xlwb.Save
Set objExcel = Nothing
Set xlwb = Nothing
Set xlws = Nothing
End Sub
Comment