Solved

Excel VBA Delete Columns on Multiple Sheets Using MS Access 2010

Posted on 2014-09-07
10
1,028 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

896 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

15 Experts available now in Live!

Get 1:1 Help Now