Results 1 to 13 of 13
  1. #1
    jobbie is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Apr 2014
    Posts
    46

    Clear excel rows after importing to access


    I have a macro in access which imports some rows from an excel sheet. How do I make it clear those rows from excel once they have been imported. I know I can use "Sub Workbook_Open()" inside the excel sheet but the problem is whenever anyone opens the excel sheet directly he/she ends up deleting those rows. I am using office 2007.

  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,929
    Think would have to open the Excel file as an object in VBA.

    Dim xlx As Object, xlw As Object, xls As Object
    Set xlx = GetObject(, "Excel.Application")

    Set xlw = xlx.Workbooks.Open("C:\Filename.xls")
    Set xls = xlw.Worksheets("WorksheetName")
    xls.Range("B11:B15").Value = ""

    Might find this site of interest http://www.accessmvp.com/KDSnell/EXCEL_MainPage.htm
    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
    jobbie is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Apr 2014
    Posts
    46
    Quote Originally Posted by June7 View Post
    Think would have to open the Excel file as an object in VBA.

    Dim xlx As Object, xlw As Object, xls As Object
    Set xlx = GetObject(, "Excel.Application")

    Set xlw = xlx.Workbooks.Open("C:\Filename.xls")
    Set xls = xlw.Worksheets("WorksheetName")
    xls.Range("B11:B15").Value = ""

    Might find this site of interest http://www.accessmvp.com/KDSnell/EXCEL_MainPage.htm

    Thanks for your help. I made one small change to your code as it was giving me a runtime error 429. I replaced line 2 of your code with this
    Code:
    Set xlx = New Excel.Application
    This is the code I have used:

    Code:
    Sub TestSub()
        On Error GoTo closeit
        Dim xlx As Object, xlw As Object, xls As Object
        
        Set xlx = New Excel.Application
        Set xlw = xlx.Workbooks.Open("C:\Test.xlsm")
        Set xls = xlw.Worksheets("Sheet1")
        RowCount = xls.Cells(xls.Rows.Count, "A").End(xlUp).Row
        xls.Range(Cells(2, 1), Cells(RowCount + 1, 4)).ClearContents
        xlw.Close SaveChanges:=True
    closeit:
        Set xlx = Nothing
    End Sub
    It seems the code runs perfectly but when I try to open the excel file directly it doesn't. I have to then go into task manager and kill the process which says EXCEL.EXE*32 and only then does the file open.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Perhaps you could try replacing:
    Code:
        xlw.Close SaveChanges:=True
    closeit:
        Set xlx = Nothing
    with:
    Code:
        xlw.Save
    closeit:
        Set xls = Nothing
        xlw.Close
        Set xlw = Nothing
        Set xlx = Nothing

  5. #5
    jobbie is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Apr 2014
    Posts
    46
    Quote Originally Posted by ItsMe View Post
    Perhaps you could try replacing:
    Code:
        xlw.Close SaveChanges:=True
    closeit:
        Set xlx = Nothing
    with:
    Code:
        xlw.Save
    closeit:
        Set xls = Nothing
        xlw.Close
        Set xlw = Nothing
        Set xlx = Nothing
    It doesn't seem to work. I gives an error "462 The remote server machine doesn't exist or is unavailable". I modified the code as below to show me the error:

    Code:
    closeit:
        MsgBox Err.Number & vbCrLf & Err.Description
        Set xls = Nothing    
        xlw.Close
        Set xlw = Nothing
        xlx.Quit
        Set xlx = Nothing

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by jobbie View Post
    It doesn't seem to work. I gives an error "462 The remote server machine doesn't exist or is unavailable". I modified the code as below to show me the error:
    Perhaps the error is from code previous to those lines. I do not get any error with the following.

    Code:
        Dim xlx As Object
        Dim xlw As Object
        Dim xls As Object
        
        Set xlx = New Excel.Application
        Set xlw = xlx.Workbooks.Open("C:\Test\ExcelFiles\ExcelFiles.xlsx")
        Set xls = xlw.Worksheets("Joe")
        
        xls.Name = "Bob"
        
        xlw.Save
    closeit:
        Set xls = Nothing
        xlw.Close
        Set xlw = Nothing
        Set xlx = Nothing
    If you make a reference to Microsoft Excel XX.X Object Library you can use Intelisense to help you with your range. Using the reference, you can declare your variables like this.

    Code:
        Dim xlx As Excel.Application
        Dim xlw As Workbook
        Dim xls As Worksheet
        Dim RowCount As Range

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I have encountered the issue of persistent Excel process and sometimes cannot figure out why it won't close. Review http://forums.aspfree.com/microsoft-...el-413629.html
    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.

  8. #8
    jobbie is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Apr 2014
    Posts
    46
    Quote Originally Posted by June7 View Post
    I have encountered the issue of persistent Excel process and sometimes cannot figure out why it won't close. Review http://forums.aspfree.com/microsoft-...el-413629.html
    well what's the solution? All I want is once the macro imports the data from excel it should then proceed to delete/clear all the rows except the headers in the excel sheet... would really appreciate your help

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    This worked for me:

    Dim xlx As Excel.Application
    Dim xlw As Workbook
    Dim xls As Worksheet
    Set xlx = New Excel.Application
    Set xlw = xlx.Workbooks.Open("C:\Temp\Book1.xlsx")
    Set xls = xlw.Worksheets("Sheet1")
    xls.Range("A2:B3").Value = ""
    xlw.Close True
    xlx.Quit
    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.

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by June7 View Post
    This worked for me:

    Dim xlx As Excel.Application
    Dim xlw As Workbook
    Dim xls As Worksheet
    Set xlx = New Excel.Application
    Set xlw = xlx.Workbooks.Open("C:\Temp\Book1.xlsx")
    Set xls = xlw.Worksheets("Sheet1")
    xls.Range("A2:B3").Value = ""
    xlw.Close True
    xlx.Quit
    Yah, I have had issues before too. As long as you close things up and destroy the objects, there should not be any memory leaks. The exception is when the application hangs because you pass it bad code. Sometimes Access won't even throw an error but Excel does not like the code you give it. I stopped having memory leaks after I started using early binding and declaring almost every Object prior to executing methods and setting properties.

  11. #11
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I also have had problems with closing and quitting Excel from Access code. I think it is a timing issue - Excel used to hang all of the time;
    now I add a "DoEvents" line and it seems to have taken care of the problem

    Code:
    Dim xlx As Excel.Application
        Dim xlw As Workbook
        Dim xls As Worksheet
        Set xlx = New Excel.Application
        Set xlw = xlx.Workbooks.Open("C:\Temp\Book1.xlsx")
        Set xls = xlw.Worksheets("Sheet1")
        xls.Range("A2:B3").Value = ""
        xlw.Close True
        DoEvents
        xlx.Quit
    Apparently, if the Excel workbook takes a long tome to save before closing, the quit command has already been executed. Excel doesn't get the quit command from Access, thus having to into taskmanager to kill the process.

    (your results may vary....)

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    That makes perfect sense, Steve.

  13. #13
    jobbie is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Apr 2014
    Posts
    46
    Quote Originally Posted by June7 View Post
    This worked for me:

    Dim xlx As Excel.Application
    Dim xlw As Workbook
    Dim xls As Worksheet
    Set xlx = New Excel.Application
    Set xlw = xlx.Workbooks.Open("C:\Temp\Book1.xlsx")
    Set xls = xlw.Worksheets("Sheet1")
    xls.Range("A2:B3").Value = ""
    xlw.Close True
    xlx.Quit
    Thanks this worked for me too.

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

Similar Threads

  1. importing multi- rows excel table into access
    By ahmed sami in forum Access
    Replies: 1
    Last Post: 10-28-2014, 01:08 PM
  2. Replies: 2
    Last Post: 06-04-2014, 11:12 AM
  3. Importing Excel File - Getting 7 extra blank rows each import
    By eking002 in forum Import/Export Data
    Replies: 4
    Last Post: 06-13-2013, 09:15 AM
  4. Replies: 10
    Last Post: 12-28-2012, 02:06 PM
  5. Importing Excel into Access
    By Nel1975 in forum Access
    Replies: 3
    Last Post: 12-30-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