Solved

Access Specific Cell in Excel from VB.NET

Posted on 2014-01-07
4
411 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

930 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

11 Experts available now in Live!

Get 1:1 Help Now