Consulting

Results 1 to 10 of 10

Thread: finder

  1. #1
    VBAX Regular aoc's Avatar
    Joined
    Apr 2007
    Location
    Istanbul
    Posts
    90
    Location

    finder

    hi

    I attached a file. numbers in sheet 2 will be checked if they are in sheet one.
    it is not important to have same format, it is enugh to find correct number in the searched sheet 1. if found, it will write ok next to each cell in sheet one.


    for example I want to search 0532 612 23 61 in sheet 2 in sheet 1, if it is in cell A2 in sheet 1 ( A2 value 905326122361 or 05326122361 ) it will write OK. so it is a character comparing, can it be with a formula or code.
    OSMAN

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,068
    Location
    I take it that any excess characters will always be to the left of the core set of characters?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Ho aoc,

    See attached. Code marked '//Begin optional ... //End optional can be deleted.
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  4. #4
    VBAX Regular aoc's Avatar
    Joined
    Apr 2007
    Location
    Istanbul
    Posts
    90
    Location
    DEAR RBRHODES

    thank you very much for the code. my original list contains same phone numbers as well. I mean that it will write ok to all 05324025310 not only one time. assume there are 145000 phone numbers, 1300 of them are 05324025310, so I will see 1300 ok written. Can you please revise it
    OSMAN

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Building on DR's code
    [vba]
    Option Explicit
    Sub GetNum()
    Dim cel As Range
    Dim rng As Range
    Dim FindRow As Long
    Dim Lastrow As Long
    Dim GetVal As String
    Dim wsNumFind As Worksheet
    Dim wsNumFound As Worksheet
    Dim FirstAddress As String
    Dim c As Range
    Dim x As Long
    'Sheets
    Set wsNumFind = Sheet2
    Set wsNumFound = Sheet1
    With wsNumFind
    'Clear old
    wsNumFound.Columns("B:B").ClearContents
    .Columns("B:B").ClearContents
    'Get last row of data to search for
    Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
    'Set range of data to search for
    Set rng = .Range("A1:A" & Lastrow)
    'Do all
    For Each cel In rng
    x = 0
    'Get cell value w/o spaces
    GetVal = Replace(cel, " ", "")
    'Allow not found error
    On Error Resume Next
    With Worksheets("Sheet1").Columns(1)
    Set c = .Find(GetVal, LookIn:=xlValues, LookAt:=xlPart)
    If Not c Is Nothing Then
    FirstAddress = c.Address
    Do
    x = x + 1
    c.Offset(, 1) = "OK" & " - " & cel
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> FirstAddress
    If x > 0 Then cel.Offset(0, 1) = "Found " & x & " times"
    FirstAddress = ""
    Else
    cel.Offset(0, 1) = "Not found"
    End If
    End With
    Next cel
    End With

    Set cel = Nothing
    Set rng = Nothing
    Set wsNumFind = Nothing
    Set wsNumFound = Nothing
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    [EDIT] "By George, I think MD got it" the OP wants it to post OK 1300 times...
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  7. #7
    VBAX Regular aoc's Avatar
    Joined
    Apr 2007
    Location
    Istanbul
    Posts
    90
    Location
    hi mdmackillop

    there is something wrong with your code.

    can you check the attached file and test ? it is enough to write " ok " in sheet 1 column B and "found" in sheet 2 column B
    OSMAN

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Works OK for me when I put my code in your workbook.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    ...not 'something wrong', per se...

    I think (tentatively) that the OP wants just "OK" on Sheet1 and just "Found" on Sheet2?

    Is this what you need:

    [VBA]
    Option Explicit
    Sub GetNum()
    Dim cel As Range
    Dim rng As Range
    Dim FindRow As Long
    Dim Lastrow As Long
    Dim GetVal As String
    Dim wsNumFind As Worksheet
    Dim wsNumFound As Worksheet
    Dim FirstAddress As String
    Dim c As Range
    Dim x As Long
    'Sheets
    Set wsNumFind = Sheet2
    Set wsNumFound = Sheet1
    With wsNumFind
    'Clear old
    wsNumFound.Columns("B:B").ClearContents
    .Columns("B:B").ClearContents
    'Get last row of data to search for
    Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
    'Set range of data to search for
    Set rng = .Range("A1:A" & Lastrow)
    'Do all
    For Each cel In rng
    x = 0
    'Get cell value w/o spaces
    GetVal = Replace(cel, " ", "")
    'Allow not found error
    On Error Resume Next
    With Worksheets("Sheet1").Columns(1)
    Set c = .Find(GetVal, LookIn:=xlValues, LookAt:=xlPart)
    If Not c Is Nothing Then
    FirstAddress = c.Address
    Do
    x = x + 1
    '//OK only
    c.Offset(, 1) = "OK"
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> FirstAddress
    '//Found only
    If x > 0 Then cel.Offset(0, 1) = "Found"
    FirstAddress = ""
    End If
    End With
    Next cel
    End With

    Set cel = Nothing
    Set rng = Nothing
    Set wsNumFind = Nothing
    Set wsNumFound = Nothing
    End Sub


    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hmmmmm
    Hopefully you've got it right. I wonder though, without the additional information for testing purposes, how one checks 4770 values written into 83000 rows. In my experience, not checking carefully will lead quickly to disaster.

    Osman
    o DR has kindly adjusted the code. I'd expect you to attempt to fix such a simple issue. We are here to assist, not simply to provide solutions. Please refer to our FAQ.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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