Consulting

Results 1 to 6 of 6

Thread: VBA and vlookup

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location

    Post VBA and vlookup

    Morning all,

    is it me being dumb or just not enough coffee yet...

    Ok so being 8am this morning got asked to try clean up a report, thought ye no problem be easy, work that vba magic on it.

    So i wrote a quick test code
    [vba]

    Sub proDelete()

    Range("B1").Select

    Do Until Selection.Value = ""

    If Selection.Value = "UNEEDED" Then
    'Selection.EntireRow.Delete
    MsgBox "Delete ME"
    Else
    Selection.Offset(1, 0).Select
    MsgBox "Keep ME"
    End If
    Loop

    Range("A1").Select

    End Sub

    [/vba]
    but this wasnt working.. to find out the the data in Column J is all in fact VLOOKUPs

    [vba]
    =VLOOKUP(F2,'Data'!A:B,2,FALSE)
    [/vba]

    now i ve bever had to deal with VBA and VLOOKUPs together so, just wondering if thers an quick easy way to read the VLOOKUPS Value its returned like "UNEEDED"

    THANKS IN ADVANCE
    Last edited by gunny2k9; 04-23-2012 at 02:15 AM.

  2. #2
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location

    Lightbulb

    ok nevermind lol think i have it working now .... LOL i think its me being dumb, 3 cups of coffe later ;P

    [vba]
    Sub proDelete()

    Range("J1").Select
    Do Until Selection.Value = ""

    If Selection.Value = "UNEEDED" Then
    'Selection.EntireRow.Delete
    MsgBox "Delete ME"
    Selection.Offset(1, 0).Select
    Else
    Selection.Offset(1, 0).Select
    'MsgBox "Keep ME"
    End If
    Loop

    Range("A1").Select

    End Sub
    [/vba]

    and can use something along the lines of (found on google, which i ve modded - not perfect)

    [vba]
    Sub test()

    result = [VLOOKUP(F2,'Route Data'!A:B,2,FALSE)]
    Debug.Print result

    If result = "UNEEDED" Then
    Debug.Print "Delete"
    Else
    Debug.Print "Stays"
    End If

    End Sub
    [/vba]

    SIMPLES !!

    but if anyone has any other ideas as i m in no way the best at vba i ll leave this thread open as handy to have this info for others

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not sure about the logic of that code at all.

    I would do it like this

    [vba]
    Sub proDelete()

    Application.ScreenUpdating = False

    lastrow = Cells(Rows.Count, "B").End(xlUp).Row

    For i = lastrow To 2 Step -1

    If Cells(i, "B").Value = "UNEEDED" Then

    Rows(i).Delete
    End If
    Loop

    Application.ScreenUpdating = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location
    i ll give that ago lol...

    was just about to reply hmm think i need a better code as just run that and relized the report is 63977 rows OH DEAR !!

    PS this report isnt for me it for someone else, just trying to see if i can find vba code to help clean it as in reduce the data.

    cheers

  5. #5
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location
    Quote Originally Posted by xld
    Not sure about the logic of that code at all.

    I would do it like this

    [vba]
    Sub proDelete()

    Application.ScreenUpdating = False

    lastrow = Cells(Rows.Count, "B").End(xlUp).Row

    For i = lastrow To 2 Step -1

    If Cells(i, "B").Value = "UNEEDED" Then

    Rows(i).Delete
    End If
    Loop

    Application.ScreenUpdating = True
    End Sub
    [/vba]
    get a Loop with out a Do error btw :P

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, should be Next i not Loop towards the end.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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