Results 1 to 4 of 4
  1. #1
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67

    Excel Automation Select Range Only with Data


    Have some Access code using Excel Automation that pastes data from a query into Excel. I want to then be able to select all data that is in column F..starting with F1..

    The data in column F will vary, but will be contiguous. So, at any given run of the code, column F could be populated from F1:F5 or at another time F1:F10 etc..

    Once selected, I want to shade the range..

    Any Access code automation help for this is appreciated..!

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    record a macro in excel and do just what you said.

    then, in access, add that code after your "application object." section of the code. it works the exact same way.

    for your info, ranges can be referenced from top to bottom in one column or row, IF they have no blank cells in the middle of the range, like this:
    Code:
    range("a1", range("a1").end(xldown))
    and for rows of course, it changes to "xltoright" (or something like that!)

  3. #3
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    Quote Originally Posted by ajetrumpet View Post
    record a macro in excel and do just what you said.

    then, in access, add that code after your "application object." section of the code. it works the exact same way.

    for your info, ranges can be referenced from top to bottom in one column or row, IF they have no blank cells in the middle of the range, like this:
    Code:
    range("a1", range("a1").end(xldown))
    and for rows of course, it changes to "xltoright" (or something like that!)
    ok, so i added the part of the code that you suggested (bolded below) and tried to run just up to that part without setting the shading yet. However, I get an error when it tries to process that part of the code:

    Method 'range of object' _Global failed

    Note: I added the select at the end as Access wouldn't allow the code without adding something at the end (got compile "expected = " error)


    Private Function fcnExport()
    On Error GoTo Err_cmdExporttoExcel_Click
    Dim automApp As Excel.Application
    Dim xlWksht As Excel.Worksheet
    Dim xlWkbook As Excel.Workbook
    Dim rs As DAO.Recordset
    Dim db As Database
    Dim strSQL As String
    Dim strPath As String
    Dim strFP As String 'file path
    Dim strFN As String 'file rpt name
    Dim strDT As String 'file name date tag
    Dim strFE As String 'file extention
    Dim lngRecCount As Long
    Dim iCols As Integer
    Set db = CurrentDb
    Set automApp = CreateObject("Excel.Application")
    'strPath = CurrentProject.Path
    strFP = "c:\6481\"
    strFN = "5753_Monthly_IFP_Billing_"
    strDT = Format(Date, "yyyymm")
    strFE = ".xls"
    strPath = strFP & strFN & strDT & strFE
    strSQL = "Select * from qry_output_Metric_Final"
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    With rs
    .MoveLast
    lngRecCount = .RecordCount
    .MoveFirst
    End With
    With automApp
    .Workbooks.Add
    .DisplayAlerts = False
    .Visible = True

    For iCols = 0 To rs.Fields.Count - 1
    .Cells(1, iCols + 1).Value = rs.Fields(iCols).Name 'changed ".cells(2, icols + 1)" from 2 to 1
    Next
    .Cells.Range("A1:G1").Font.Bold = True
    .Columns.Range("A:G").HorizontalAlignment = xlCenter
    .Columns.Range("F1:F7").HorizontalAlignment = xlLeft
    .Cells.Range("A1:A1").Interior.Color = 12632256
    .Cells.Range("B1:B1").Interior.Color = 8421631
    .Cells.Range("C1:C1").Interior.Color = 16776960
    .Cells.Range("D1:D1").Interior.Color = 16744576
    .Cells.Range("E1:E1").Interior.Color = 8454016
    .Cells.Range("F1:G1").Interior.Color = 33023
    .Range("A2").CopyFromRecordset rs

    .Range("F1", Range("F1").End(xlDown)).Select

    .Columns.AutoFit
    .ActiveWorkbook.SaveAs FileName:=strPath
    End With

    Exit_cmdExporttoExcel_Click:
    On Error Resume Next
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    automApp.Quit
    Exit Function
    Err_cmdExporttoExcel_Click:
    MsgBox Err.Description
    Resume Exit_cmdExporttoExcel_Click

    End Function

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    i don't know what to tell you bud. my trial found this to be just fine in exceL
    Code:
    Range("e4", Range("e4").End(xlDown)).Select
    no error.

    you know, if you have the EXCEL referenced checked in the vba references, you don't have to use early binding like that, and use the createobject() method. or wait...that's late binding, right?

    whatever...anyway, why not try it the other way? honestly, I don't know you get the error, especially since the ".range" part of the line works on the line right above, which is "copyfromrecordset".

    so I would try to use the excel ref and see if the intellisense for ".select" appears. If it doesn't you know you have a reference problem or something.

    I'm sorry I can't much at this point. I don't do much automation between xls and mdb right now.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Excel Automation Autofit Column (or Cell?) Width
    By orcinus in forum Programming
    Replies: 3
    Last Post: 02-09-2011, 11:44 AM
  2. Exporting to Excel with Automation using QueryDef
    By Niezels in forum Import/Export Data
    Replies: 3
    Last Post: 09-26-2010, 05:55 PM
  3. How to import named range from excel
    By timpepu in forum Import/Export Data
    Replies: 1
    Last Post: 02-19-2010, 11:26 AM
  4. Replies: 2
    Last Post: 12-03-2009, 08:08 AM
  5. Data from automation
    By Mstef in forum Import/Export Data
    Replies: 0
    Last Post: 01-05-2009, 10:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums