Solved

Access Specific Cell in Excel from VB.NET

Posted on 2014-01-07
4
401 Views
Last Modified: 2014-01-07
Imports Excel = Microsoft.Office.Interop.Excel    

Public Sub WhatsMyVal()
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(TextBox1.Text)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        Dim myString = xlWorkSheet.Range("A1").Value.ToString

        MsgBox("this is myString: " & myString)
    End Sub

Open in new window


is it possible to access cells in a worksheet by using something like xlWorkSheet.cells(1,1) instead of  xlWorkSheet.Range("A1").Value.ToString.  It seems it would be easier to navigate through a worksheet if I could just use 2 numbers.

Thanks for your help!!!
0
Comment
Question by:slightlyoff
  • 2
4 Comments
 
LVL 9

Expert Comment

by:s_chilkury
ID: 39762927
How about this:

// Get a reference to cell A1 and write the formatted value to the console.
            IRange a1 = worksheet.Cells["A1"];
            Console.WriteLine("A1={0}", a1.Text);
            // Get a reference to B2 and write the formula / value / text to the console.
            IRange b2 = worksheet.Cells[1, 1];
            Console.WriteLine("B2 Formula={0}, Value={1}, Text={2}", b2.Formula, b2.Value, b2.Text);


Also check the source here:

http://support.microsoft.com/kb/219151
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39762930
Yes, it is very easy... you already gave the answer.

Just go... xlWorkSheet.Dells(1,1).Value.ToString
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39762940
I'm a moron.  It didn't give "Value" in intelisense, so I figured it wasn't an option and didn't try it.  Thanks for the help!!!
0
 
LVL 40
ID: 39762986
For some reason, IntelliSense gives up after a few references (dots) when working with COM objects from .NET.

For instance, in something like Workbooks(1).Worksheets(1).Cells(1,1).Font.Bold = True, IntelliSense usually doest not go all the way.

However, the following will go all the way:

Dim rng As Range = Workbooks(1).Worksheets(1).Cells(1,1)
rng.Font.Bold=True
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

759 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