Privateer
Registered User.
- Local time
- , 20:59
- Joined
- Aug 16, 2011
- Messages
- 193
I am exporting data from Access to Excel and following that up with formulas and formatting. The code is in Access and works on the Excel object and it works fine. The problem is it doesn't work a second time. I have to run compact and repair in Access and then the code works again. Below is what I am opening/creating at the beginning of the procedure and what I am closing. It fails when I try to assign a range to the CSR variable with error 1004: Method 'Cells' of object '_Global' failed
I don't have a menu with the code tag option so here it is.
Dim xl As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim CSR As Range 'Current sheet range
Dim TRN As Long 'The row number
Dim TCN As Long 'The column number
Set xl = New Excel.Application
Set xlBook = xl.Workbooks.Open(FilePath)
Set xlSheet = xlBook.Worksheets(1)
xl.Visible = True
xlSheet.Activate
TRN = xlSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
TCN = xlSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
The row number is 17 and the column is 10
The code below selects range("E18:J18") and the next line puts a formula in those five cells that totals row 2 through 17, like "=SUM(E2:E17)" The cool thing is that the columns change automatically so that the formula in column J is "=SUM(J2:J17)"
Set CSR = xlSheet.Range(Cells(TRN + 1, TCN - 5), Cells(TRN + 1, TCN)) <-fails here
With CSR
.FormulaR1C1 = "=SUM(R[" & ((TRN * -1) + 1) & "]C:R[-1]C)"
...
End With
The CSR variable has been assigned many ranges up to this point, but it blows up on this specific line when the code is executed a second time.
My exit code is below. I have error trapping going on, so this runs whether there is an error or not.
CleanExit:
xl.DisplayAlerts = True
Set CSR = Nothing
xlBook.Close True
Set xlSheet = Nothing
Set xlBook = Nothing
xl.Quit
Set xl = Nothing
Exit Sub
So I think I am closing everything I am opening, but something is blowing up. I have used the watch window to follow the CSR variable and seen nothing that indicates a problem, but there is a lot being displayed. I am hoping someone else has experienced this unique feature in Access and can help me. As always, any suggestions would be much appreciated.
Privateer
I don't have a menu with the code tag option so here it is.
Dim xl As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim CSR As Range 'Current sheet range
Dim TRN As Long 'The row number
Dim TCN As Long 'The column number
Set xl = New Excel.Application
Set xlBook = xl.Workbooks.Open(FilePath)
Set xlSheet = xlBook.Worksheets(1)
xl.Visible = True
xlSheet.Activate
TRN = xlSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
TCN = xlSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
The row number is 17 and the column is 10
The code below selects range("E18:J18") and the next line puts a formula in those five cells that totals row 2 through 17, like "=SUM(E2:E17)" The cool thing is that the columns change automatically so that the formula in column J is "=SUM(J2:J17)"
Set CSR = xlSheet.Range(Cells(TRN + 1, TCN - 5), Cells(TRN + 1, TCN)) <-fails here
With CSR
.FormulaR1C1 = "=SUM(R[" & ((TRN * -1) + 1) & "]C:R[-1]C)"
...
End With
The CSR variable has been assigned many ranges up to this point, but it blows up on this specific line when the code is executed a second time.
My exit code is below. I have error trapping going on, so this runs whether there is an error or not.
CleanExit:
xl.DisplayAlerts = True
Set CSR = Nothing
xlBook.Close True
Set xlSheet = Nothing
Set xlBook = Nothing
xl.Quit
Set xl = Nothing
Exit Sub
So I think I am closing everything I am opening, but something is blowing up. I have used the watch window to follow the CSR variable and seen nothing that indicates a problem, but there is a lot being displayed. I am hoping someone else has experienced this unique feature in Access and can help me. As always, any suggestions would be much appreciated.
Privateer