Solved

VB.Net / MS Excel - Select and Clear Range

Posted on 2014-02-07
4
3,865 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

810 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