+ Reply to Thread
Results 1 to 2 of 2

Pivot Query Report

  1. #1
    Registered User
    Join Date
    07-29-2010
    Location
    Multan
    MS-Off Ver
    Excel 2010
    Posts
    38

    Pivot Query Report

    I have recorded a macro to first delete all cells in a sheet and then create Pivot table from Union Query .
    After recording , when i run it, it gives me error.

    Following is the macro

    Can anyone correct it, and also change its path to active workbook

    Sub PivotQuery_Macro()
    '
    ' Macro13 Macro
    '

    Cells.Delete Shift:=xlUp
    Cells.Delete Shift:=xlUp
    Cells.Delete Shift:=xlUp
    Range("A1").Select
    Workbooks("Automated LD Record R-2.xls").Connections.Add2 _
    "Query from Excel Files1", "", Array(Array( _
    "ODBC;DSN=Excel Files;DBQ=C:\USERS\ZUBAI_000\Desktop\Automated LD Record R-2.xls;DefaultDir=C:\USERS\ZUBAI_000\Desktop;DriverId=790;M" _
    ), Array("axBufferSize=2048;PageTimeout=5;")), _
    "SELECT `FO$`.Outage__Type, `FO$`.Month, `FO$`.`Wt#Add#Cap#__MWh`, `FO$`.`Wt#Cap#__MWh`, `FO$`.`Wt#Total#__MWh`" & Chr(13) & "" & Chr(10) & "FROM `C:\USERS\ZUBAI_000\Desktop\Automated LD Record R-2`.`FO$` `FO$`" & Chr(13) & "" & Chr(10) & "UNION ALL" & Chr(13) & "" & Chr(10) & "SELECT `PFO R1$`.Outage__Type, `PFO R1$`.Month, `PFO R1$`.`Wt#Add#Cap#__MWh`, `PFO R1$`.`Wt#Cap#__MWh`, `PFO R1$`.`Wt#Total#__MWh`" & Chr(13) & "" & Chr(10) & "FROM `C:\USERS\ZUBAI_000\Desktop\Automated" & _
    "rd R-2`.`PFO R1$` `PFO R1$`" & Chr(13) & "" & Chr(10) & "", 2
    With ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, Version:= _
    xlPivotTableVersion10)
    .Connection = Array(Array( _
    "ODBC;DSN=Excel Files;DBQ=C:\USERS\ZUBAI_000\Desktop\Automated LD Record R-2.xls;DefaultDir=C:\USERS\ZUBAI_000\Desktop;DriverId=790;M" _
    ), Array("axBufferSize=2048;PageTimeout=5;"))
    .CommandType = xlCmdSql
    .CommandText = _
    "SELECT `FO$`.Outage__Type, `FO$`.Month, `FO$`.`Wt#Add#Cap#__MWh`, `FO$`.`Wt#Cap#__MWh`, `FO$`.`Wt#Total#__MWh`" & Chr(13) & "" & Chr(10) & "FROM `C:\USERS\ZUBAI_000\Desktop\Automated LD Record R-2`.`FO$` `FO$`" & Chr(13) & "" & Chr(10) & "UNION ALL" & Chr(13) & "" & Chr(10) & "SELECT `PFO R1$`.Outage__Type, `PFO R1$`.Month, `PFO R1$`.`Wt#Add#Cap#__MWh`, `PFO R1$`.`Wt#Cap#__MWh`, `PFO R1$`.`Wt#Total#__MWh`" & Chr(13) & "" & Chr(10) & "FROM `C:\USERS\ZUBAI_000\Desktop\Automated" & _
    "rd R-2`.`PFO R1$` `PFO R1$`" & Chr(13) & "" & Chr(10) & ""
    .Cr
    Table TableDestination:="Summary!R1C1", TableName:="PivotTable2", _
    DefaultVersion:=xlPivotTableVersion10
    End With
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Outage__Type")
    .Orientation = xlColumnField
    .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Month")
    .Orientation = xlRowField
    .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Outage__Type")
    .PivotItems("Totals").Visible = False
    .PivotItems("(blank)").Visible = False
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Month")
    .PivotItems("(blank)").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("Wt#Total#__MWh"), "Count of Wt#Total#__MWh", _
    xlCount
    With ActiveSheet.PivotTables("PivotTable2").PivotFields( _
    "Count of Wt#Total#__MWh")
    .Caption = "Wt. Total MWh"
    .Function = xlSum
    .NumberFormat = "#,##0.000"
    End With
    End Sub

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Pivot Query Report

    joogibabu,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1