Results 1 to 2 of 2
  1. #1
    kazman101 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2011
    Posts
    1

    Red face Delete first row and Column for excel import

    Hi Guy's

    I need to import some data into Access from an Excel sheet, the data was extracted from Business Objects which seems to have an annoying limitation where it adds a blank column and a Blank row into the spreadsheet before adding the data

    is there a way to remove a Blank Column and Row before importing the data into Access

    as it is now my column headings are on the second row and start in the second column

    can anyone help?

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You can try this code from MS Access:

    Code:
    Sub main()
    
    Dim objExcel As Object
    Dim objWorkbook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    
    Set objExcel = CreateObject("excel.application")
    On Error GoTo ERRHANDLER
    
    Debug.Print "CP1"
    Set objWorkbook = objExcel.Workbooks.Open("c:\test_file_3.xls")
    Debug.Print "CP2"
    Set objSheet = objWorkbook.Worksheets("FirstSheet")
    
    Debug.Print "CP3"
    objSheet.Columns("A:A").Select
    Debug.Print "CP4"
    Selection.Delete 'shift:=xlToLeft
    Debug.Print "CP5"
    objSheet.Rows("1:1").Select
    Debug.Print "CP6"
    Selection.Delete 'shift:=xlUp
    Debug.Print "CP7"
    
    objWorkbook.Save
    objExcel.Application.Quit
    Set objExcel = Nothing
    Set objWorkbook = Nothing
    Set objSheet = Nothing
    
    Exit Sub
    
    ERRHANDLER:
        Debug.Print Err.Number & " " & Err.Description
        objExcel.Application.Quit
        Set objExcel = Nothing
        Set objWorkbook = Nothing
        Set objSheet = Nothing
        
    End Sub
    I haven't figured it out but for some reason it works sometimes and doesn't others and I think it's because it's not correctly closing the file or something but it does work when it runs without this error:

    91 Object variable or With block variable not set

    on this line

    Selection.Delete 'shift:=xlToLeft

    but at least you have a starting point

    I suspect it will work for you if you don't open the file at all you just try to process it.

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

Similar Threads

  1. Replies: 3
    Last Post: 12-21-2010, 11:52 AM
  2. Import to Excel from Access column of LOOKUP data type
    By Derek in forum Import/Export Data
    Replies: 1
    Last Post: 12-06-2010, 06:27 PM
  3. Can't delete a column (field)
    By jlmnjem in forum Access
    Replies: 2
    Last Post: 09-20-2010, 12:02 PM
  4. Replies: 2
    Last Post: 08-24-2010, 03:32 PM
  5. VBA to open excel, import on close of excel
    By bdaniel in forum Programming
    Replies: 0
    Last Post: 03-20-2010, 02:45 PM

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