Solved

Getting a row number from Excel using VB.NET

Posted on 2012-03-29
2
620 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:mwmiller78
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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