Solved

Excel VBA Delete Columns on Multiple Sheets Using MS Access 2010

Posted on 2014-09-07
10
1,090 Views
Last Modified: 2014-09-08
I need to delete multiple columns on all Excel worksheets using MS Access VBA. Thanks
0
Comment
Question by:shieldsco
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:Subramani N
ID: 40308571
Include the below code in the vba,

With Sheets(sheet_name)
     .Columns("A:AN").EntireColumn.Delete
End With

Or

With Sheets(sheet_name)
     Range("A:D,F:J,M:O,S:V").Delete
End With
0
 
LVL 33

Expert Comment

by:Norie
ID: 40308572
Which columns on which sheets?
0
 
LVL 2

Expert Comment

by:Computer Egghead
ID: 40308573
This should do the trick.  Run it as many times as you wish.  Remember that once you delete a column, the columns will shift, that is, if you delete column D, what was column E becomes D.
Single column example, just enter D
Multiple column example, enter D:F
Public Sub Macro1()
    Dim columnRange As String
    columnRange = InputBox("Column(s) to delete? (D:F)")
    DeleteColumnsAllWorksheets columnRange
End Sub

Private Sub DeleteColumnsAllWorksheets(columnRange As String)
    Dim w As Worksheet
    For Each w In ThisWorkbook.Worksheets
        w.Range(columnRange).EntireColumn.Delete Shift:=xlToLeft
    Next w
    Set w = Nothing
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:aikimark
ID: 40308597
@shieldsco

Do you want the columns physically deleted from the worksheet(s) or do you want to clear the cell data in those columns?
0
 
LVL 2

Expert Comment

by:Computer Egghead
ID: 40308661
Just realized you asked for MS Access 2010 (If you are in Access, it is Access VBA, not Excel VBA), so I'm amending my answer.  

1. Add a reference to Microsoft Office 14.0 Object Library and Microsoft Excel 14.0 Object Library

2. Add this to a module in your Access app:

Option Explicit
Option Compare Database

Public Sub ProcessExcelFile()
    Dim o As Object
    Set o = Application.FileDialog(msoFileDialogFilePicker)
    o.AllowMultiSelect = False
    o.Title = "Select an Excel workbook"
    o.Filters.Clear
    o.Filters.Add "Excel workbooks", "*.xl*"
    If o.Show Then
        Dim sPath As String
        sPath = o.SelectedItems(1)
    End If
    Dim columnRange As String
    columnRange = InputBox("Column(s) to delete? (Example, D:F)")
    DeleteColumnsAllWorksheets sPath, columnRange
End Sub

Private Function DeleteColumnsAllWorksheets(filePath As String, columnRange As String) As Boolean
    Dim bResult As Boolean: bResult = True
    Dim oApp As Excel.Application
    Dim oWB As Excel.Workbook
    Dim oWS As Excel.Worksheet

    On Error GoTo ErrorHandler
    Set oApp = New Excel.Application
    Set oWB = oApp.Workbooks.Open(filePath)
    For Each oWS In oWB.Worksheets
        oWS.Range(columnRange).EntireColumn.Delete Shift:=xlToLeft
    Next oWS
    oWB.Close True
    Set oWS = Nothing
    Set oWB = Nothing
    Set oApp = Nothing
    GoTo EndProcedure
    
ErrorHandler:
    bResult = False
EndProcedure:
    DeleteColumnsAllWorksheets = bResult
End Function

Open in new window

0
 

Author Comment

by:shieldsco
ID: 40310119
ComputerEgghead - how do I access my spreadsheet located on a network drive (the spreadsheet name will always be the same)?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40310291
@shieldsco

Please answer my question
0
 

Author Comment

by:shieldsco
ID: 40310645
aikimark - I want the columns physically deleted from the worksheet(s)
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40310690
The VBA code will look something like this
Dim oXL as object
dim wkb as object
dim wks as object
set oxl=createobject("excel.application")
set wkb=oxl.workbooks.open("\\servername\path to workbook\workbookname.xlsx")
wkb.worksheets("sheetname").range("C:D").Delete
wkb.close true
oxl.quit
set oxl=nothing

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question