Results 1 to 2 of 2
  1. #1
    gasmaskman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    23

    Delete Columns in Excel using Access VBA


    Using VBA in Access, I am trying to open an excel file, detect all the columns in sheet1 that have no data in rows 2 through to the end of the file (headers in row 1), and delete these entire columns. Right now when I run the code I receive an error: "run-time error '1004': method 'Cells' of object '_global' failed" on the line of code that starts with, "If wf.CountA". Thank you in advance for any help.

    Code:
    Private Sub cmdDelete_Click()
        Dim xl As New Excel.Application
        Dim wf As Excel.WorksheetFunction
        Dim wk As Excel.Workbook
        Dim ws As Excel.Worksheet
        Dim strPath As String
        Dim j As Long
         
        strPath = "C:\" & SVCnumber1 & "\" & SVCnumber1 & " Output" & ".xls"
        Set wk = xl.Workbooks.Open(strPath)
        Set ws = wk.Sheets("sheet1")
        Set wf = xl.WorksheetFunction
        
        For j = 40 To 1 Step -1
            If wf.CountA(ws.Range(Cells(2, j), Cells(Rows.Count, j))) = 0 Then
                ws.Columns(j).Delete
            End If
        Next j
         
        wk.Save
        wk.Close
        xl.Quit
        Set xl = Nothing
    End Sub
    Last edited by gasmaskman; 02-04-2014 at 09:43 AM.

  2. #2
    gasmaskman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    23
    I finally got the code to work! I'm posting the code just in case anyone else runs into a similar problem.

    Code:
        Dim xl As New Excel.Application
        Dim wf As Excel.WorksheetFunction
        Dim wk As Excel.Workbook
        Dim ws As Excel.Worksheet
        Dim strPath As String
        Dim j As Long
         
        strPath = "C:\" & SVCnumber1 & "\" & SVCnumber1 & " Output" & ".xls"
        Set wk = xl.Workbooks.Open(strPath)
        Set ws = wk.Sheets("sheet1")
        Set wf = xl.WorksheetFunction
        
        For j = 40 To 1 Step -1
            If wf.CountA(ws.Range(ws.Cells(2, j), ws.Cells(ws.Rows.Count, j))) = 0 Then
                ws.Columns(j).Delete
            End If
        Next j
         
        wk.Save
        wk.Close
        xl.Quit
        Set xl = Nothing

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

Similar Threads

  1. Replies: 8
    Last Post: 06-04-2013, 01:37 PM
  2. Export columns (table) from access to excel
    By dacodac in forum Programming
    Replies: 4
    Last Post: 01-30-2013, 04:27 AM
  3. Delete first row and Column for excel import
    By kazman101 in forum Import/Export Data
    Replies: 1
    Last Post: 07-18-2011, 06:53 AM
  4. Replies: 0
    Last Post: 09-18-2009, 07:33 AM
  5. Replies: 0
    Last Post: 04-29-2009, 04:27 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