Rx_
Nothing In Moderation
- Local time
- Today, 06:48
- Joined
- Oct 22, 2009
- Messages
- 2,803
Situation: Using Range object in VBA code. It runs perfect the first time. The second or more times, there is a Global error. Close Access, and it runs the first time, and errors on the second or more times.
Set Excel into an object - and destroying all objexts (i.e. set objExcel = Nothing) hs no effect.
Found an article that showe this to be a known problem for the Range object (including Charts) when the report is run over and over again.
This fixed it for me: Add ActiveSheet. in front of Range.
From now on, like eating chicken soup when your sick ... " it couldn't hurt"
this cost me an entire morning. Found the solution elsewhere and wanted to make it avilable to our community.
If you find this kind of tip useful, please add to my reputation. It will encourage me to add more.
Running code from inside Access.
Remember to set a Reference to the Excel Application.
Set Excel into an object - and destroying all objexts (i.e. set objExcel = Nothing) hs no effect.
Found an article that showe this to be a known problem for the Range object (including Charts) when the report is run over and over again.
This fixed it for me: Add ActiveSheet. in front of Range.
From now on, like eating chicken soup when your sick ... " it couldn't hurt"
this cost me an entire morning. Found the solution elsewhere and wanted to make it avilable to our community.
If you find this kind of tip useful, please add to my reputation. It will encourage me to add more.
Running code from inside Access.
Remember to set a Reference to the Excel Application.
Code:
[B]Set objXL = New Excel.Application[/B]
[B]The code below then calls methods of the objXL object[/B]
Code fragment sorts through Excel column " M " returned by CopyRecordset method. The code removes the hidden quotes that make the data "text" which prevents the SubTotal(101, <range>) from working.
The Excel cell format will not convert the values to numbers in this case.
Before adding "AceiveSheet." this would run perfectlly once.. then fail every time afterwards.
' the ActiveSheet. in the next statement prevents a global error if
' this code is run a second time
Set rnOmrade = NothingSet rnOmrade = objXL.ActiveSheet.Range(objXL.Range("M5"), objXL.Range("M6536").End(xlUp))vaData = rnOmrade.Value
For i = 1 To UBound(vaData) ' data base record set returns "Pending", blank or a number
If (vaData(i, 1) = "Pending") Then
vaData(i, 1) = "Pending"
ElseIf ((vaData(i, 1) * 1) = 0) Then
vaData(i, 1) = ""
Else
vaData (i, 1) = vaData(i, 1) * 1
End If
'vaData(i, 1) = IIf(IsNumeric(vaData(i, 1) * 1), vaData(i, 1) * 1, vaData(i, 1))
Next i
arrayrnOmrade.Value = vaData