Results 1 to 4 of 4

Thread: Finding a row in Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Question Finding a row in Excel

    Hey,

    I need to make a section of code that will search for a term like "1000" and find the row that contains it. (there will never be two the same) and then save that row.

    I have this for finding the last row edited:

    Code:
      Dim oExcel As Object
            Dim oBook As Object
            Dim oSheet As Object
            Dim LastRow As Long
            Dim lastjn As Integer
            Dim newjb As Integer
    
            oExcel = CreateObject("Excel.Application")
            oBook = oExcel.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\job.xlsx")
            oSheet = oBook.worksheets(1)
    
            LastRow = oSheet.UsedRange.Rows.Count
    So im guessing it will be simular, just searching for a term instead of last edited.

    Thanks
    VS 2017 - Need as much help as you can give (write it all for me!)

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Finding a row in Excel

    Take a look at this option:
    Code:
    Dim cell As String
    For x As Integer = 1 To range.Rows.Count
        For y As Integer = 1 To range.Columns.Count
            cell = DirectCast(range.Cells(x, y), Excel.Range).Value.ToString()
            If cell = "1000" Then
                'This cell equals 1000, do something with it
            End If 
        Next
    Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Finding a row in Excel

    Quote Originally Posted by dday9 View Post
    Take a look at this option:
    Code:
    Dim cell As String
    For x As Integer = 1 To range.Rows.Count
        For y As Integer = 1 To range.Columns.Count
            cell = DirectCast(range.Cells(x, y), Excel.Range).Value.ToString()
            If cell = "1000" Then
                'This cell equals 1000, do something with it
            End If 
        Next
    Next
    Do I need to Dim range as something?
    VS 2017 - Need as much help as you can give (write it all for me!)

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Finding a row in Excel

    Yeah, range would be the cells that you wanted to iterate through.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width