Solved

Excel VBA Delete Columns on Multiple Sheets Using MS Access 2010

Posted on 2014-09-07
10
1,136 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

728 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