Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2008, 12:46 PM
Registered User
 
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel VBA

Hey all,

New to the forum and need a little help. What i am trying to do is export data from Access ADP into an excel file after which i want to group (OutlineLevel=2) all the cells in column A that are the same. I can do this if I use an access report then export it to excel.

Here is what i got so far:
Code:
Dim sql As String

sql = "SELECT tblHeaders.ProNumber, SCAC_A AS SCAC, ShipDate_A AS ShipDate, upper(ShipperName_A) AS Shipper," & _
        " upper(ShipperAddress_A) AS [S Address], upper(ShipperCity_A) AS [S City], upper(ShipperState_A) AS [S ST]," & _
        " ShipperZip_A AS [S Zip], upper(ConsigneeName_A) AS Consignee, upper(ConsigneeAddress_A) AS [C Address], upper(ConsigneeCity_A) AS [C City]," & _
        " upper(ConsigneeState_A) AS [C ST], ConsigneeZip_A AS [C Zip], LineType_A as [Type], NMFC_A + '-' + NMFCSub_A AS NMFC," & _
        " NMFCClass_A AS CLASS, ItemWeight_A AS Weight, Charges_A AS Charges, Description_A As Description" & _
        " FROM tblHeaders JOIN tblLineItems ON tblHeaders.HeaderID = tblLineItems.HeaderID" & _
        " WHERE (Mode_A = 'l') AND (ClientID in (49320,9797,57885,39685)) AND (SubmissionDate BETWEEN '4/29/2008' AND '4/29/2008')" & _
        " and charges_A is not null AND LineType_A in ('I','D','FL')" & _
        " Order by ShipDate_A , ShipperName_A, ConsigneeName_A"

    Text1.Value = sql

    ExportSQLToExcel (sql)



End Sub

Sub ExportSQLToExcel(ByVal sSQL As String)

    Dim rs As ADODB.Recordset
    Dim xlApp As Excel.Application
    Dim iCol As Integer
    Set rs = New ADODB.Recordset
    Dim y As Integer
    Dim maxcell As Range
    Dim strPronumber As String



    StatusBar "Retrieving Data from Database..."
    DoCmd.Hourglass True
    rs.Open sSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly

    If rs.RecordCount > 65535 Then
        MsgBox "More than 65535 records returned. First 65535 rows exported."
    End If

    If rs.RecordCount > 0 Then
        StatusBar "Exporting Data to Excel..."
        Set xlApp = CreateObject("Excel.Application")
        xlApp.Workbooks.Add
'Copies Column Names
        For iCol = 0 To rs.fields.Count - 1
            xlApp.Cells(1, iCol + 1) = rs.fields(iCol).Name
        Next
'Copies Data
        xlApp.Range("A2").CopyFromRecordset rs
        xlApp.Cells.Select
        xlApp.Cells.EntireColumn.AutoFit
'Changes Row 1 in Excel to a Dark Gray shade
        xlApp.Rows(1).Select
        xlApp.Selection.Interior.ColorIndex = 15       'Set background to Dark Gray
        xlApp.Selection.HorizontalAlignment = xlCenter

'NEED CODE TO GROUP COLUMN A CELLS THAT ARE THE SAME


'Reset Cursor to cell A1
        xlApp.Range("A1").Select
        xlApp.Visible = True
        xlApp.Sheets(1).Select
    Else
        MsgBox "No Records returned"
    End If
    ClearStatusBar
    DoCmd.Hourglass False
Any help would be greatly apprecaited!!!

 
Old May 3rd, 2008, 07:14 AM
Authorized User
 
Join Date: Mar 2008
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this:
Code:
xlApp.ActiveSheet.UsedRange.Rows.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _
      Replace:=True, PageBreaks:=False, SummaryBelowData:=True

To customize the Subtotal method's parameters, consult the On-Line VBA Help






Similar Threads
Thread Thread Starter Forum Replies Last Post
Code works in Excel VBA but not Access VBA fossx Access VBA 2 May 21st, 2007 08:00 AM
New to Excel VBA anukagni Excel VBA 0 June 24th, 2006 05:51 AM
Converting excel data to Access using excel VBA ShaileshShinde VB Databases Basics 1 April 26th, 2006 07:57 AM
VBA Excel jayanp Access VBA 3 December 16th, 2004 03:18 AM
Excel VBA to SQL & back to VBA edesousa Excel VBA 1 June 1st, 2004 02:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.