By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.