+ Reply to Thread
Results 1 to 6 of 6

Where would I define a specific column

Hybrid View

  1. #1
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Where would I define a specific column

    Hi all,

    I have this bit of code which works for what I need, but I can't find where to specify a certain column.

    If I only want it to search column D for the required value, where would I specify this?

     
    
    ub LookforText()
    Dim bk1 As Workbook, sh1 As Worksheet, cell As Range
    Dim bk2 As Workbook, sh2 As Worksheet, r As Range
    
    Set bk1 = Workbooks("Credit Hire Review Assistant 3.10.xlsm")
    Set sh1 = bk1.Worksheets("Settlement")
    Set cell = sh1.Range("F5")    ' this remembers the cell where the search string is
      ' and can be used to produce the value on demand
    
    Set bk2 = Workbooks.Open("S:\Claims\Credit Hire\Credit Hire Spreadsheet\C Hire MI Sept 2011-.xlsx")
    Set sh2 = bk2.Worksheets("Sheet1")
    Set r = sh2.Cells.Find(What:=cell.Value, _
      After:=ActiveCell, _
      LookIn:=xlValues, _
      LookAt:=xlWhole, _
      SearchOrder:=xlByRows, _
      SearchDirection:=xlNext, _
      MatchCase:=False)
    If r Is Nothing Then
     MsgBox "Not found"
    Else
     Application.Goto Reference:=r
    End If
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Where would I define a specific column

    Change this line

    Set r = sh2.Cells.Find(What:=cell.Value, _
    as

    Set r = sh2.Range("D:D").Find(What:=cell.Value, _

  3. #3
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Where would I define a specific column

    Hi,

    Thanks for the reply, I just tried amending as you suggest but now when it runs the script, it comes up with a "Run-time error '13' and says Type mismatch.

    Any ideas?

    Thanks again

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Where would I define a specific column

    You Pl Try This.

    Change this

    Set r = sh2.Cells.Find(What:=cell.Value, _
      After:=ActiveCell, _
      LookIn:=xlValues, _
      LookAt:=xlWhole, _
      SearchOrder:=xlByRows, _
      SearchDirection:=xlNext, _
      MatchCase:=False)
    By

    Set r = sh2.Range("D:D").Find(What:=cell.Value)
    Or by


    Set r = sh2.Range("D:D").Find(What:=cell.Value, _
      After:=Range("D1"), _
      LookIn:=xlValues, _
      LookAt:=xlWhole, _
      SearchOrder:=xlByRows, _
      SearchDirection:=xlNext, _
      MatchCase:=False)

    The error is due to activecell is not in D column.

  5. #5
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Where would I define a specific column

    That is absolutely perfect, it speeds up the search by about 10 seconds - which is quite a long time when you are staring at a screen! Many thanks.

    As an aside, I'm trying to also put a
    Rows(ActiveCell.Row).Select
    in there after it has found the correct cell but it comes up with a 1004 error, I don't suppose you would also know where this would need to go?

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Where would I define a specific column

    I did not follow where you are using the above code.
    You can try


    ActiveCell.EntireRow.Select

    If your problem is solved,mark the thread solved.

    With regards,
    kvsmrthy
    Last edited by kvsrinivasamurthy; 05-09-2012 at 09:44 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1