Solved

Getting a row number from Excel using VB.NET

Posted on 2012-03-29
2
616 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now