473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Delete multiple columns from Excel Spreadsheet from VB.Net application?

Can someone help with code to delete multiple columns from an excel
spreadsheet? I know which columns I need to delete. The code below will
delete a single column but I'm not sure how to delete multiple columns. I'm
tried experimenting with Dim rg As Excel.Range = xlSheet.Columns("B, D, G,
K, L") but no joy. Thanks in advance
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Open("D:\August_Sales.xls")
xlSheet = xlBook.Worksheets(1)
Dim rg As Excel.Range = xlSheet.Columns("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlApp)
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing

Oct 4 '08 #1
3 20808
Is there some reason that you wouldn't just repeat

Dim rg As Excel.Range = xlSheet.Columns("B")
rg.Select()
rg.Delete()

for the other columns, using the letters for those columns (working from
right to left, of course)?

"Will" <wi************@here.comwrote in message
news:ul**************@TK2MSFTNGP03.phx.gbl...
Can someone help with code to delete multiple columns from an excel
spreadsheet? I know which columns I need to delete. The code below will
delete a single column but I'm not sure how to delete multiple columns.
I'm tried experimenting with Dim rg As Excel.Range = xlSheet.Columns("B,
D, G, K, L") but no joy. Thanks in advance
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Open("D:\August_Sales.xls")
xlSheet = xlBook.Worksheets(1)
Dim rg As Excel.Range = xlSheet.Columns("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlApp)
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing
Oct 4 '08 #2
That "could" work. The problem is that when you delete column "B", the
columns to the right are moved left. You need to be aware of that when
addressing the subsequent columns. A way arround that would be to delete
columns from right to left, as L, K, G, D, B.
"James Hahn" <jh***@yahoo.comwrote in message
news:uD**************@TK2MSFTNGP04.phx.gbl...
Is there some reason that you wouldn't just repeat

Dim rg As Excel.Range = xlSheet.Columns("B")
rg.Select()
rg.Delete()

for the other columns, using the letters for those columns (working from
right to left, of course)?

"Will" <wi************@here.comwrote in message
news:ul**************@TK2MSFTNGP03.phx.gbl...
>Can someone help with code to delete multiple columns from an excel
spreadsheet? I know which columns I need to delete. The code below will
delete a single column but I'm not sure how to delete multiple columns.
I'm tried experimenting with Dim rg As Excel.Range = xlSheet.Columns("B,
D, G, K, L") but no joy. Thanks in advance
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Open("D:\August_Sales.xls")
xlSheet = xlBook.Worksheets(1)
Dim rg As Excel.Range = xlSheet.Columns("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlApp)
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing
Oct 4 '08 #3
That's why I included in the original message the comment "(working from
right to left, of course)?"
"Family Tree Mike" <Fa************@ThisOldHouse.comwrote in message
news:%2***************@TK2MSFTNGP03.phx.gbl...
That "could" work. The problem is that when you delete column "B", the
columns to the right are moved left. You need to be aware of that when
addressing the subsequent columns. A way arround that would be to delete
columns from right to left, as L, K, G, D, B.
"James Hahn" <jh***@yahoo.comwrote in message
news:uD**************@TK2MSFTNGP04.phx.gbl...
>Is there some reason that you wouldn't just repeat

Dim rg As Excel.Range = xlSheet.Columns("B")
rg.Select()
rg.Delete()

for the other columns, using the letters for those columns (working from
right to left, of course)?

"Will" <wi************@here.comwrote in message
news:ul**************@TK2MSFTNGP03.phx.gbl...
>>Can someone help with code to delete multiple columns from an excel
spreadsheet? I know which columns I need to delete. The code below
will delete a single column but I'm not sure how to delete multiple
columns. I'm tried experimenting with Dim rg As Excel.Range =
xlSheet.Columns("B, D, G, K, L") but no joy. Thanks in advance
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Open("D:\August_Sales.xls")
xlSheet = xlBook.Worksheets(1)
Dim rg As Excel.Range = xlSheet.Columns("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlApp)
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing
Oct 20 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting...
1
by: smonczka | last post by:
I have a DTS package that needs to refresh data in 3 separate Excel spreadsheets on a daily basis. The problem is that unless I manually delete the previous day's data, it appends rather than...
1
by: poohnie08 | last post by:
i have a excel spreadsheet showing staff name, date,work hour, ot hour, slot1, slot2, slot3, slot4 and others). The "()" will keep repeating from day 1 until end of month. eg in excel spreadsheet,...
2
by: Regnab | last post by:
I've got my code working so that it'll count the number of columns in the table and move across (eg Range A-P and then range Q-W). Problem is when I get to the end of the single letters and get...
1
by: Jack M | last post by:
I am creating an excel spreadsheet from a data grid, there are more than 255 columns (approx 660), and so I need to create threee tabs or sheets. I have done this in VB6 but could use some help in...
2
by: Scott M. Lyon | last post by:
I currently am working on a VB.NET application that imports data from an Excel spreadsheet that the customer filled in. I used the following code to use ODBC to read the data from the...
6
by: Karl Richards | last post by:
I am attempting to delete duplicate rows in a spreadsheet using the Excel object. Does anyone have any idea how to do this? I've looked everywhere that I can find on the Web and have not been...
0
by: sysmanint1 | last post by:
I am a total neophyte at Visual Basic but found the following post and reply from Clint concerning a dynamic range. Also, have never "posted" to a discussion I have made a macro that works on...
1
by: DThreadgill | last post by:
I have a spreadsheet with about 35 tabs that I need to import into one table in Access. I found this link http://www.thescripts.com/forum/thread650662-import+multiple+tabs.html and changed some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.