Results 1 to 5 of 5
  1. #1
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44

    Help with excel macro from Access

    I am trying to delete rows in an excel workbook that is currently being written to via my Access VBA (it is open using the following code).



    Code:
       strPath = "C:\test\test.xlsb"
       Set ApXL = CreateObject("Excel.Application")
       'Debug.Print strPath
       Set xlWBk = ApXL.Workbooks.Open(strPath)
       ApXL.Visible = False


    I have the following code which currently runs in the workbook itself but I would like help converting it to run in the Access VBA code while I write all the data to it. Does anyone know now I can convert the following code to run in access?

    Thank you in advance.

    Code:
    Dim iRowCountA As Integer, iRowCountB As Integer
        
        iRowCountA = Range(Sheets("sheet1").Range("A2"), Sheets("sheet1").Range("A2").End(xlDown)).Rows.Count
        iRowCountB = Range(Sheets("sheet2").Range("B10"), Sheets("sheet2").Range("B10").End(xlDown)).Rows.Count
    
        Sheets("sheet2").Range("A" & iRowCountA + 10 & ":A" & iRowCountB + 10 - 1).EntireRow.Delete

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The code needs to reference the Excel object opened by VBA. Something like:

    iRowCountA = xlWBk.Sheets("sheet1").Range("A2"), xlWBk.Sheets("sheet1").Range("A2").End(xlDown)).Ro ws.Count
    iRowCountB = xlWBk.Sheets("sheet2").Range("B10"), xlWBk.Sheets("sheet2").Range("B10").End(xlDown)).R ows.Count

    xlWBk.Sheets("sheet2").Range("A" & iRowCountA + 10 & ":A" & iRowCountB + 10 - 1).EntireRow.Delete
    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
    allenjasonbrown@gmail.com is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    44
    Thanks for the reply. I am getting a compile error "syntax error". Any idea?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The few times I have written code in Access VBA to manipulate an Excel object were challenging. Getting the referencing of sheets, ranges, cells right usually takes me a lot of trial and error. Might even have to create a sheet object. Maybe instead of Sheets, try Worksheets.

    Unfortunately, I don't have functional code now. If I have time tomorrow will try some.
    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.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I needed to put the Range reference back. This works:

    iRowCountA = Range(xlWBk.Sheets("sheet1").Range("A2"), xlWBk.Sheets("sheet1").Range("A2").End(xlDown)).Ro ws.Count
    iRowCountB = Range(xlWBk.Sheets("sheet2").Range("B10"), xlWBk.Sheets("sheet2").Range("B10").End(xlDown)).R ows.Count
    xlWBk.Sheets("sheet2").Range("A" & iRowCountA + 10 & ":A" & iRowCountB + 10 - 1).EntireRow.Delete

    Only problem - sometimes leaves an EXCEL.EXE process running in task manager. Even when I manually close the Excel application. Even after deleting the proceses, sometimes get error 462, 'The remote server machine does not exist or is unavailable'. I have encountered this before, sometimes I can resolve, sometimes I give up. Right now I give up and you are on your own.
    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. Run Excel macro from Access VBA
    By ragsgold in forum Programming
    Replies: 29
    Last Post: 01-29-2013, 06:55 PM
  2. Replies: 18
    Last Post: 09-04-2012, 12:06 PM
  3. Excel Import Macro Help in Access
    By vennies83 in forum Import/Export Data
    Replies: 5
    Last Post: 11-02-2011, 09:24 AM
  4. Replies: 1
    Last Post: 10-15-2010, 06:09 AM
  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