Solved

Excel VBA Delete Columns on Multiple Sheets Using MS Access 2010

Posted on 2014-09-07
10
1,000 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now