Link to home
Start Free TrialLog in
Avatar of kwarden13
kwarden13

asked on

Export data from Access to Specific Excel cells (Named Range)

Hi

I am currently using a transfer spreadsheet code to export a qry to a specific excel tab, however, I would like to export to a specific "starting" cell.

I think I need to build a dynamic range. I have been playing with this for hours. Please I need HELP!
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

what do you mean by building a dynamic range?

if cells destination will vary you can
* create named ranges and use this
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QueryName", xlPath, True, "NameOfrange"

* use recordsets

Set rs = db.OpenRecordset("Query1")
Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add
Set Sheet = xlObj.activeworkbook.Sheets(1)
   
'copy the headers
Dim iRow, iCol
iRow = 1
    For iCol = 0 To rs.Fields.Count - 1
        Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
    Next


Sheet.Range("A2").CopyFromRecordset rs  'this copy just the data

Open in new window




Avatar of kwarden13
kwarden13

ASKER

The start cell of the destination will not vary, however how many records does. So there could be 1 or 500 records depending on the month.
all the fields are the same and I would like it NOT to export with headers and keep the formatting of the workbook everytime and just replace the named range

I generally use code similar to what capricorn1 shows in his code, above.
ok I will give it a try will it meet all my needs. Just want to make sure before I waste more time on this.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So the named range only works 1 time. If I tell i to export twice, it creates a new tab. Also, the headers are still there
So 1st try the named range was: ='Past Due'!$A$6:$O$1048576

2nd try the named range was: ==#REF!$A$1:$O$938
export function is
 
Function Delinquents()

DoCmd.TransferSpreadsheet acExport, , "qryDelRepCAN", "File_Path", True, "Canada_Past_Due"

End Function

Open in new window

how are you creating your named range?

Are you using the named range for anything besides this export?

If so, you could use Cap1's code (minus the headings) to export so that the record 1, column 1 of your recordset appears in cell "A2" (or whatever cell you want), then you could delete the named range (also via VBA), and then recreate it via code.

You might also consider making the range dynamic, see the solution in:  https://www.experts-exchange.com/questions/22468473/redefining-a-range-in-excel-that-is-controlled-via-access.html?sfQueryTermInfo=1+10+30+creat+dynam+excel+name+rang+vba
No I am just using it for Export. I am creating it by clicking on the Formulas tab > Define Name > and highlight the cells I want to be in the range.

Personally, I'd get rid of the range altogether and use code similar to Cap1's.
I will try it