Consulting

Results 1 to 12 of 12

Thread: Solved: Find value in column

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    Solved: Find value in column

    I have values in K column which are "Prost" or "Zaseden" and now I want to check if zaseden exist in this column and if exist then Msgbox "Yes it exists"

    thnx

  2. #2
    VBAX Regular
    Joined
    Jun 2006
    Posts
    27
    Location
    Hey saban

    Found this in a reply by mdmackillop so if it works he's the man to thank.

    [vba]
    Sub FindLookup()
    Dim ThsBook As Workbook, c As Range
    Application.ScreenUpdating = False
    Set c = Columns("K").Find(What:="Zaseden")
    If Not c Is Nothing Then
    MsgBox "Yes it exists"
    Exit Sub
    End If
    MsgBox "Zaseden not found"
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Actually the problem is that it finds value "Zasedenost" which is the header of column and then if not c is nothing... gives me always "yes it exists"

    Actually it does not work it doesnt find value "zaseden" at all

    any ideas
    Thnx
    Last edited by saban; 06-29-2006 at 03:22 AM.

  4. #4
    VBAX Regular
    Joined
    Jun 2006
    Posts
    27
    Location
    I see. It seems to search for that string of letters regarldess if there is text either side. When you removed the header of the column and run it again - are you saying there was zaseden in the column but it didn't find it because I can't reproduce that.

    Need to get it to find zaseden and only zaseden. Sorry about that.

  5. #5
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Yeah i need to find only "zaseden"

    thnx

  6. #6
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    use this

    Sub FindLookup()
    Dim ThsBook As Workbook, c As Range
    Application.ScreenUpdating = False
    Set c = Columns("K").Find(What:="Zaseden", LookAt:=xlWhole)
    If Not c Is Nothing Then
    MsgBox "Yes it exists"
    Exit Sub
    End If
    MsgBox "Zaseden not found"
    Application.ScreenUpdating = True
    End Sub

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The Find Method in Help is worth studying. (Header address here is assumed as K2)
    [VBA]
    Sub FindLookup()
    Dim ThsBook As Workbook, c As Range, Header As Range
    Set Header = [K2]
    Application.ScreenUpdating = False
    Set c = Columns("K").Find(What:="Zaseden", after:=Header, LookIn:=xlValues, _
    LookAt:=xlWhole, MatchCase:=False)
    If Not c Is Nothing And Not c.Address = Header.Address Then
    MsgBox "Yes it exists"
    Exit Sub
    End If
    MsgBox "Zaseden not found"
    Application.ScreenUpdating = True
    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'

  8. #8
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    sorry (I forgot vba style)
    [VBA]
    Sub FindLookup()
    Dim ThsBook As Workbook, c As Range
    Application.ScreenUpdating = False
    Set c = Columns("K").Find(What:="Zaseden", LookAt:=xlWhole)
    If Not c Is Nothing Then
    MsgBox "Yes it exists"
    Exit Sub
    End If
    MsgBox "Zaseden not found"
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

  9. #9
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Thnx guys I really appreciate this

    but why do i get error when "zaseden is not found" error is at line
    [VBA]Set c = Columns("K").Find(What:="Zaseden", after:=Header, LookIn:=xlValues, _
    [/VBA]

  10. #10
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    [VBA]
    Sub FindLookup()
    Dim ThsBook As Workbook, c As Range, Header As Range
    Set Header = [K2]
    Application.ScreenUpdating = False
    On Error GoTo NotFound
    Set c = Columns("K").Find(What:="Zaseden", after:=Header, LookIn:=xlValues, _
    LookAt:=xlWhole, MatchCase:=False)
    If Not c Is Nothing And Not c.Address = Header.Address Then
    MsgBox "Yes it exists"
    Exit Sub
    End If
    NotFound:
    MsgBox "Zaseden not found"
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

  11. #11
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Thnx guys it works

    stay cool

  12. #12
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Saban please mark this thread as 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
  •