Results 1 to 5 of 5
  1. #1
    Rob_U is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2015
    Posts
    10

    Running Excel VB Code From Access

    Hi

    I have an issue with an access form I have created, which allows any csv file to be imported without any errors. The only issue is the csv file being imported, needs to have a field titled 'Ref' with auto populated numbers in the data before importing, which means I manually need to add this each time (I can't do this after importing, as this is required to keep the structure)



    I've created a vb code in excel, which does this job for me successfully. However is there any way of running this vb code from access?
    I know how to run a macro which is already saved in the excel file from access, but this needs to work with any csv file to import

    Code:
     Sub Ref()
    
    'Inserts an auto number
    Dim r As Range
    Columns(1).Insert Shift:=xlToRight
    For Each r In ActiveSheet.UsedRange.Rows
    Cells(r.Row, 1) = r.Row
    Next
    
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "Ref"
    
    End Sub
    Thank you for your help

  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,972
    Bing: Access run Excel macro
    Review http://datapigtechnologies.com/blog/...xcel-workbook/
    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
    Rob_U is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2015
    Posts
    10
    Thank you for the link, but I already understand how to do that.
    The issue is that the macro is not saved in the excel document, so the actual macro script needs to be located in access (I assume the only way?)
    Is it possible to call a macro script saved in the access database to run on any excel document?

    Thanks
    Rob

  4. #4
    trevor40's Avatar
    trevor40 is offline Advanced db Manager
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    402
    [QUOTE=Rob_U;276635]Thank you for the link, but I already understand how to do that.
    The issue is that the macro is not saved in the excel document, so the actual macro script needs to be located in access (I assume the only way?)
    Is it possible to call a macro script saved in the access database to run on any excel document?
    the easiest way is to just use access vba to do it all,

    first create a link to the excel file
    then use the code you showed to update the sheet
    the import the updated file

    you will find examples of doing the above in my previous posts.
    i have posted many answers on how to manipulate excel via access.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    trevor, AFAIK, cannot edit cells in a linked worksheet. Do you actually mean open objects in VBA?

    Open Excel objects in VBA then can manipulate the objects to edit the content. Yes, this code can be dynamic to work on any worksheet that has the same structure expected by the code. Just need to dynamically specify the workbook path/filename.
    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.

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

Similar Threads

  1. Running Access Query from Excel VBA
    By dunc723 in forum Programming
    Replies: 3
    Last Post: 06-03-2014, 06:05 AM
  2. Replies: 1
    Last Post: 05-01-2012, 03:52 AM
  3. Running excel macro's from withing Access
    By zippy483 in forum Programming
    Replies: 5
    Last Post: 03-08-2011, 11:47 AM
  4. Replies: 3
    Last Post: 10-04-2010, 01:31 PM
  5. Running excel macro from access
    By timpepu in forum Programming
    Replies: 1
    Last Post: 02-26-2010, 11:32 PM

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