Solved

VB.Net / MS Excel - Select and Clear Range

Posted on 2014-02-07
4
4,055 Views
Last Modified: 2014-02-12
Using Microsoft.Office.Interop.Excel.Application within my VB.Net application.

Trying to select a worksheet range ( A3: G LastRow ) and clear all in that range:

        Dim objExcel1 As New Microsoft.Office.Interop.Excel.Application
        objExcel1 = CreateObject("Excel.Application")
        objExcel1.Workbooks.Open("C:\Spreadsheet\Carbon.xlsx")
        objExcel1.Visible = False
        objExcel1.DisplayAlerts = False

        Dim CarbonWS As Worksheet, CarbonNewRng As Range
        CarbonWS = objExcel1.Sheets("CarbonScrap")
        CarbonNewRng = CarbonWS.Range(CarbonWS.Cells(3, 1), CarbonWS.Cells _(CarbonWS.Rows.Count, 7))
        CarbonNewRng.Clear()

        objExcel1.Workbooks(1).SaveAs("C:\Spreadsheet\Carbon.xlsx")
        objExcel1.Quit()
        objExcel1 = Nothing

Above doesn't work, what will?
0
Comment
Question by:Tim313
[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
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39842150
Hi,

Maybe CarbonNewRng.Clear instead of CarbonNewRng.Clear()

Regards
0
 
LVL 35

Expert Comment

by:mvidas
ID: 39842152
Hi,

Your error looksl ike it is likely on this line:
CarbonNewRng = CarbonWS.Range(CarbonWS.Cells(3, 1), CarbonWS.Cells(CarbonWS.Rows.Count, 7))

Open in new window

It looks like you're just clearing columns C:G, so you can instead use:
CarbonNewRng = CarbonWS.Columns("C:G")

Open in new window


You shouldn't need that entire block though. Instead of making the worksheet and range variables, just use the single line:
objExcel1.Sheets("CarbonScrap").Columns("C:G").Clear()

Open in new window

Should take care of it for you.
You may need to add .ActiveWorkbook in there:
objExcel1.ActiveWorkbook.Sheets("CarbonScrap").Columns("C:G").Clear()
Matt
0
 

Accepted Solution

by:
Tim313 earned 0 total points
ID: 39842599
Thanks to all for your replies, but I found my own answer:

Dim CarbonWS As Worksheet
CarbonWS = objExcel1.Sheets("CarbonScrap")
CarbonWS.Range(CarbonWS.Cells(3, 1), CarbonWS.Cells(CarbonWS.Rows.Count, 7)).Clear()

Please note I stated:

"Trying to select a worksheet range ( A3: G LastRow ) and clear all in that range"

not just columns C:G.
0
 

Author Closing Comment

by:Tim313
ID: 39852806
Found the solution on my own. Suggestions provided by others would not work.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

730 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