Solved

Access Specific Cell in Excel from VB.NET

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

739 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