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
Solved

Getting a row number from Excel using VB.NET

Posted on 2012-03-29
2
628 Views
Last Modified: 2012-04-13
I am using the following to get a value of a specific cell from an Excel spreadsheet. Is there a way to find the first instance of a particular string in columnA and determine the row number?

           
Dim oXL As Microsoft.Office.Interop.Excel.Application, oBook As Microsoft.Office.Interop.Excel.Workbook, oSheet As Microsoft.Office.Interop.Excel.Worksheet, vValue As Object

            oXL = New Microsoft.Office.Interop.Excel.Application
            oBook = oXL.Workbooks.Open("C:\testapp\" & strTextFileName & "result" & ".csv")
            oSheet = oBook.Worksheets(strTextFileName & "result")

            vValue = oSheet.Cells(1, 1).Value       'Get the value from cell A1
            TextBox1.Text = vValue

            oSheet = Nothing
            oBook.Close()
            oBook = Nothing
            oXL.Quit()
            oXL = Nothing

Open in new window

0
Comment
Question by:Mike Miller
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 37786753
When working with Excel (or Word for that matter), and I don't know how to do something, I just record a macro:


    Range("C2").Select
    Cells.Find(What:="John Edwards", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate

Then, I try to figure out how to replicate that functionality in .NET.
0
 
LVL 20

Assisted Solution

by:ElrondCT
ElrondCT earned 250 total points
ID: 37787942
Picking up where TheLearnedOne left off, generally the functions become methods of your worksheet. The xlXXX values are constants defined by the Excel namespace, but unfortunately it can be difficult to find exactly what the path through the namespace and classes is. However, if you're taking the defaults, you can just skip them. So in your case, you could get what you want with something like the following:

            Dim strFind as String = "xyz"
            Dim intRowEnd As Integer = oSheet.Range("A1").End(Excel.XlDirection.xlDown).Row
            Dim intFindRow As Integer = oSheet.Range("A1:A" & intRowEnd.ToString).Find(strFind).Row

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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