Advertisement

10.25.2007 at 09:19AM PDT, ID: 22917995
[x]
Attachment Details

Problem Accessing Excel worksheet cell value from vb.net

Asked by misdevelopers in Microsoft Visual Basic.Net, Microsoft Excel Spreadsheet Software

Tags: excel, cell, worksheet

Hello,

I have an application that refers to an existing excel workbook and I am trying to retreive values from group of cells in a worksheet using a loop.

There doesn't seem to be a problem if I use excelSheet.Range("C6").value.tostring, but if I try to use excelSheet.Cells(6,y).toString it only returns "System.__ComObject" instead of the cell value.  I can't seem to figure out why.

Here is my code (I "defactored" it for readability):

Public excelApp As Excel.Application
Public excelWb As Excel.Workbook
Public excelSheet As Excel.Worksheet
Public excelSheet As Excel.Worksheet
Public testSettings As Excel.Worksheet

excelApp = New Excel.Application
excelWb = CType(GetObject(excelPath()), Excel.Workbook

testSettings = CType(excelWb.Sheets("testSettings"), Excel.Worksheet)
excelSheet = CType(excelWb.Sheets(testSettings.Range("B5").Value), Excel.Worksheet)

      Public Sub loadFieldValues(ByVal testName As String)

            Dim x As Integer      ' rows
            Dim y As Integer      ' columns

            For y = 3 To 25
                  If excelSheet.Cells(6, y).ToString = testName Then    'here is the problem (.Range works but I can't loop with it)
                        Exit For
                  End If
            Next
      End Sub
Start Free Trial
[+][-]10.25.2007 at 09:22AM PDT, ID: 20148876

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, Microsoft Excel Spreadsheet Software
Tags: excel, cell, worksheet
Sign Up Now!
Solution Provided By: rorya
Participating Experts: 2
Solution Grade: B
 
 
[+][-]10.25.2007 at 09:26AM PDT, ID: 20148903

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.25.2007 at 09:31AM PDT, ID: 20148956

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906