473,702 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Applicati on
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("E xcel.Applicatio n")
xlBook = xlApp.Workbooks .Open("D:\Augus t_Sales.xls")
xlSheet = xlBook.Workshee ts(1)
Dim rg As Excel.Range = xlSheet.Columns ("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime. InteropServices .Marshal.Releas eComObject(xlBo ok)
System.Runtime. InteropServices .Marshal.Releas eComObject(xlAp p)
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing

Oct 4 '08 #1
3 20838
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******** ******@TK2MSFTN GP03.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.Applicati on
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("E xcel.Applicatio n")
xlBook = xlApp.Workbooks .Open("D:\Augus t_Sales.xls")
xlSheet = xlBook.Workshee ts(1)
Dim rg As Excel.Range = xlSheet.Columns ("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime. InteropServices .Marshal.Releas eComObject(xlBo ok)
System.Runtime. InteropServices .Marshal.Releas eComObject(xlAp p)
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.co mwrote in message
news:uD******** ******@TK2MSFTN GP04.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******** ******@TK2MSFTN GP03.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.Applicati on
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("E xcel.Applicatio n")
xlBook = xlApp.Workbooks .Open("D:\Augus t_Sales.xls")
xlSheet = xlBook.Workshee ts(1)
Dim rg As Excel.Range = xlSheet.Columns ("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime. InteropServices .Marshal.Releas eComObject(xlBo ok)
System.Runtime. InteropServices .Marshal.Releas eComObject(xlAp p)
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.c omwrote in message
news:%2******** *******@TK2MSFT NGP03.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.co mwrote in message
news:uD******** ******@TK2MSFTN GP04.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******* *******@TK2MSFT NGP03.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.Colum ns("B, D, G, K, L") but no joy. Thanks in advance
Dim xlApp As Excel.Applicati on
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

xlApp = CreateObject("E xcel.Applicatio n")
xlBook = xlApp.Workbooks .Open("D:\Augus t_Sales.xls")
xlSheet = xlBook.Workshee ts(1)
Dim rg As Excel.Range = xlSheet.Columns ("B")
rg.Select()
rg.Delete()
xlBook.Save()
xlApp.Quit()
System.Runtime. InteropServices .Marshal.Releas eComObject(xlBo ok)
System.Runtime. InteropServices .Marshal.Releas eComObject(xlAp p)
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
3356
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 columns when some cells are empty. Is there a way to use the ss:Index to account for the missing <Cell elements? Thank you for any suggestions. ====================
1
9231
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 replaces. I can't delete the excel files on a daily basis, as they have to be there for the DTS package to be able to export to Excel. What I want to do is create a VBScript (ActiveX Control) to delete all the rows of data except the first row...
1
3359
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, ============================================================================== A1 |A2 A3 A4 A5 A6 A7 A8 |A9 A10 A11 | 01/02/04 |02/02/04 StaffName |Work Hr OT Hr Slot1...
2
3471
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 into AA etc. Have got an idea of how I could do it by using Chr() and having an ongoing counter which is divided by 26 when it goes past Z to work out location. However, I'd dearly like there to be an easier way. Any ideas? Another problem I'm...
1
1477
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 .Net. I dimension objExcel as a new excel application, but when I type .sheets my only option is 'Get Type'. I have tried several combinations but I hit a wall everytime. Any help will be appreciated. Thanks Jack
2
16870
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 spreadsheet, and return it in a datatable. Public Function ReadExcelDT(ByVal aFileName As String) As DataTable ' Open the Excel Spreadsheet, and using ODBC, read it into a DataTable for later processing
6
16113
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 able to find anything on it. What I have so far is: Excel.Range range = oSheet.get_Range("A1", Type.Missing); range = range.get_End(Excel.XlDirection.xlDown); string downAddress = range.get_Address(false, false, XlReferenceStyle.xlA1,...
0
6183
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 a template spreadsheet with a fixed number of columns to be "macro'd" bounded by data that changes in its number of rows. That is, the bounding data is imported to the spreadsheet before the calculated columns are run. But the rows of this data...
1
10889
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 things within the code. First off - I've written a piece of code that changes the name of each sheet (Sheet1, Sheet2, etc), deletes the blank rows and formats a couple of the columns to text and date format. It then saves and quits the application...
0
8738
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9086
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8979
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8939
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7829
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5907
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.