Solved

Getting a row number from Excel using VB.NET

Posted on 2012-03-29
2
630 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
[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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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