Solved

Getting a row number from Excel using VB.NET

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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