Hi all,
I am trying to export query to Excel from Access, manipulate it into a pivot table, format the pivot table and then export into a word document...
I have some code that I have made/taken from other places to do so but I come across a runtime error on one line of code (line 66 in the below code section). This error only happens every other time the code is run which I find odd - e.g. first time works fine, second time gets error. The error relates to me trying to change the number format for all of the value cells that are not subtotals/totals.
All help is greatly appreciated! - Sub ExportToWord()
-
-
Dim xlApp As Excel.Application
-
Dim xlWB As Excel.Workbook
-
Dim xlWS As Excel.Worksheet
-
Dim pt As PivotTable
-
Dim i As Integer
-
Dim rng1 As Range
-
Dim rng2 As Range
-
Dim ptItem As PivotItem
-
Dim fileName As String
-
Dim dataSheet As Worksheet
-
Dim pivotSheet As Worksheet
-
Dim pc As PivotCache
-
Dim x As Integer
-
Dim WordApp As Word.Application
-
Dim myDoc As Word.Document
-
-
'in my sub i have the actual path here but removed just in case ;)
-
fileName = "\test.xlsx"
-
-
'export query to excel file
-
DoCmd.TransferSpreadsheet acExport, , "qryCountThisCross", fileName, True
-
-
'open excel file
-
Set xlApp = New Excel.Application
-
-
With xlApp
-
.Visible = True
-
Set xlWB = .Workbooks.Open(fileName, , False)
-
End With
-
-
Set dataSheet = xlWB.Worksheets(1)
-
-
'set up pivot table
-
xlWB.Worksheets.Add(After:=dataSheet).Name = "PivotSheet"
-
Set pivotSheet = xlWB.Worksheets("PivotSheet")
-
-
Set pc = xlWB.PivotCaches.Create(xlDatabase, dataSheet.Range("A1:P40"))
-
Set pt = pc.CreatePivotTable(pivotSheet.Range("A1"), "PivotTable")
-
-
'edit pivot table layout
-
Set pt = pivotSheet.PivotTables("PivotTable")
-
With pt
-
With .PivotFields("Service Line")
-
.Orientation = xlRowField
-
.Position = 1
-
.PivotItems("Younger Adult").Position = 1
-
.PivotItems("OPMH").Position = 2
-
.PivotItems("Rehab").Position = 3
-
.PivotItems("Forensic & Specialist").Position = 4
-
End With
-
With .PivotFields("Location")
-
.Orientation = xlRowField
-
.Position = 2
-
End With
-
With .PivotFields("Ward Name")
-
.Orientation = xlRowField
-
.Position = 3
-
End With
-
For x = 5 To .PivotFields.Count
-
.AddDataField pt.PivotFields(x), .PivotFields(x).Name & " ", xlSum
-
Next x
-
.AddDataField pt.PivotFields(4), "Total ", xlSum
-
-
'the problem section
-
'i'm trying to make it so that if a 0 appears in the data section that is not in the subtotals
-
'or totals then it should appear in light grey
-
For i = 1 To (pt.DataFields.Count - 1)
-
Set rng1 = pt.DataFields(i).DataRange
-
For Each ptItem In pt.PivotFields("Ward Name").PivotItems
-
Set rng2 = ptItem.DataRange.EntireRow
-
'Runtime error 1004: Method 'Intersect' of object' _ Global failed
-
Intersect(rng1, rng2).NumberFormat = "#,##0;#,##0;[Color15]#,##0"
-
Next ptItem
-
Next i
-
-
.CompactLayoutRowHeader = "Ward by Service Line"
-
.DataPivotField.Caption = "Month"
-
.PivotFields(1).LayoutBlankLine = True
-
.SubtotalLocation xlAtBottom
-
End With
-
-
pt.TableRange1.Copy
-
-
'Create an Instance of MS Word
-
On Error Resume Next
-
-
'Is MS Word already opened?
-
Set WordApp = GetObject(Class:="Word.Application")
-
-
'Clear the error between errors
-
Err.Clear
-
-
'If MS Word is not already open then open MS Word
-
If WordApp Is Nothing Then Set WordApp = CreateObject(Class:="Word.Application")
-
-
'Handle if the Word Application is not found
-
If Err.Number = 429 Then
-
MsgBox "Microsoft Word could not be found, aborting."
-
GoTo CLEAR_UP
-
End If
-
-
On Error GoTo 0
-
-
'Make MS Word Visible and Active
-
WordApp.Visible = True
-
WordApp.Activate
-
-
'Create a New Document
-
Set myDoc = WordApp.Documents.Add
-
-
'Copy Excel Table Range
-
WordApp.Selection.PasteSpecial , , , , wdPasteOLEObject
-
-
xlWB.Close False
-
xlApp.Quit
-
-
CLEAR_UP:
-
Set xlWS = Nothing
-
Set xlWB = Nothing
-
Set xlApp = Nothing
-
Set myDoc = Nothing
-
Set WordApp = Nothing
-
-
Set pt = Nothing
-
Set pc = Nothing
-
Set pivotSheet = Nothing
-
Set dataSheet = Nothing
-
Set xlWB = Nothing
-
-
Set rng2 = Nothing
-
Set rng1 = Nothing
-
Set ptItem = Nothing
-
-
End Sub
I've not done what you are attempting, but I've done some Cross Office Application VBA development. I looked up the Intersect command on Microsoft's website and there was this example: - Worksheets("Sheet1").Activate
-
Set isect = Application.Intersect(Range("rg1"), Range("rg2"))
-
If isect Is Nothing Then
-
MsgBox "Ranges do not intersect"
-
Else
-
isect.Select
-
End If
From their code it looks like the Intersect Method doesn't always return an object. Which means you might want to want to test to see if there is an intersection before setting the Number Format. I didn't look at your code hard enough to see if there will always be an intersection, but this would be the place I would start.
Something like: - Dim oIntersect As Range
-
...
-
Set oIntersect = xlApp.Intersect(rng1, rng2)
-
If not oIntersect Is Nothing Then
-
oIntersect.NumberFormat = "#,##0;#,##0;[Color15]#,##0"
-
End If
2 1834
I've not done what you are attempting, but I've done some Cross Office Application VBA development. I looked up the Intersect command on Microsoft's website and there was this example: - Worksheets("Sheet1").Activate
-
Set isect = Application.Intersect(Range("rg1"), Range("rg2"))
-
If isect Is Nothing Then
-
MsgBox "Ranges do not intersect"
-
Else
-
isect.Select
-
End If
From their code it looks like the Intersect Method doesn't always return an object. Which means you might want to want to test to see if there is an intersection before setting the Number Format. I didn't look at your code hard enough to see if there will always be an intersection, but this would be the place I would start.
Something like: - Dim oIntersect As Range
-
...
-
Set oIntersect = xlApp.Intersect(rng1, rng2)
-
If not oIntersect Is Nothing Then
-
oIntersect.NumberFormat = "#,##0;#,##0;[Color15]#,##0"
-
End If
Hi jforbes,
Thanks for having a look. I would have thought that there would always be an intersection being a pivot table with a format that doesn't change. Anyway, I gave the code a go and it works like a charm. Tested it 5 times in a row with no runtime errors and correct results everytime.
Many thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: richilli |
last post by:
Hi
Any help on this would be appreciated cos its driving me insane.
I have a function in VB.NET that takes in an excel range and tries to
delete rows where the first column starts with a...
|
by: Lauren Wilson |
last post by:
Hi folks,
I have an Access 2003 app that works perfectly on my computer.
However, when we install it on OTHER computers that ALSO have access
2003 we get an error on startup:
Error:...
|
by: Chris |
last post by:
Hi,
I tried to install Access Runtime 2007 on 2 machines having Word,
Excel, Power Point 2007 but unfortunately it did not succeed. I got
the message "Installation failed" which was not helpful...
|
by: ielamrani |
last post by:
Hi,
I am getting this error when I try to export to an excel sheet. When I click on a button to export the first time it's fine, I rename the exported excel sheet and I try to export it again and I...
|
by: CoreyReynolds |
last post by:
Hey all,
I have a piece of code that dumps a bunch of data into a spreadsheet. Also rearranges it into a pivot table and then graphs the pivot table as well so my boss can get a clear view of the...
|
by: fnemo |
last post by:
I'm getting the error - Method 'Item' of object 'Forms' failed . Earlier this error was not occuring.
In the below code, first textboxes are created dynamically in the form "display_result"....
|
by: Hema Suresh |
last post by:
Hi all
I created a database via VB and saved it in excel sheet
I have 10 command buttons and 10 text box controls on the vb form and i coded in the way to get the data from the user
once the...
|
by: ChiomaJennifer |
last post by:
hi...
i have an access application in which i need to open an existing excel sheet, find a date (already in the sheet) and populate a row (with the date cell column) with either 1 or 0
this means...
|
by: bpremlatha |
last post by:
Sub WorkBook_Open()
RemoveEmptyRow
ConcatenateColumn
DeleteBlankColumns
SaveFile
End Sub
Sub SaveFile()
'ActiveWorkbook.Save
ThisWorkbook.Saved = True
|
by: hvsummer |
last post by:
Hi guy,
this is another core question.
While I can't handle with OLE error on this link...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |