Results 1 to 3 of 3
  1. #1
    derekben is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2013
    Posts
    11

    Formatting Access Export to Excel - VBA


    Hi,


    I am looking to export a table to excel from access. I would like to add a column with a header of RefNumber and it will add one every row until the last row of data, starting at 1 (1,2,3,4 ect.) I have successfully added the title and have been able to find how to calculate getting the last row but I can not get it to go down the column and add one. This is the code I have so far. ANY HELP WOULD BE AMAZING! THANK YOU.

    NOTE: oWB.Sheets(1) is referring to the export excel document being created by this button.

    oWB.Sheets(1).Range("G1").Value = "RefNumber"


    Dim x As Integer
    Dim total As Integer
    total = 1
    x = oWB.Sheets(1).UsedRange.Rows.Count


    oWB.Sheets(1).Range("G2").Value = total


    For i = 1 To x
    oWB.Sheets(1).Range("G3").Select
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = total
    oWB.Sheets(1).Range("G3").Value = total


    total = total + 1
    Next

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You keep selecting G3 within the loop so not moving anywhere.

    Faster code if you don't 'jump around' the sheet.

    Try:

    For i = 1 To x
    oWB.Sheets(1).Range("G" & i + 3) = oWB.Sheets(1).Range("G2") + i
    Next
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    derekben is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2013
    Posts
    11
    Thanks!!! That saved me a lot of time. Have a great day!!

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

Similar Threads

  1. Export Data to Excel Pivot Table Loss of Formatting
    By Damian in forum Import/Export Data
    Replies: 1
    Last Post: 05-17-2013, 07:56 PM
  2. Replies: 7
    Last Post: 04-25-2013, 03:47 PM
  3. Marco: Export to Excel w/o Formatting
    By AKoval in forum Import/Export Data
    Replies: 1
    Last Post: 03-20-2013, 09:14 AM
  4. Access VBA Excel Pivot Table Formatting
    By Tomlon in forum Programming
    Replies: 1
    Last Post: 02-07-2013, 02:28 PM
  5. Excel export formatting
    By tariq nawaz in forum Import/Export Data
    Replies: 0
    Last Post: 09-24-2012, 05:30 AM

Tags for this Thread

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