Solved

VB.Net / MS Excel - Select and Clear Range

Posted on 2014-02-07
4
3,789 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
  • 2
4 Comments
 
LVL 49

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

895 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now