+ Reply to Thread
Results 1 to 3 of 3

Thread: Application.VLookup loop not working.

  1. #1
    Registered User
    Join Date
    03-11-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    2

    Unhappy Application.VLookup loop not working.

    I've to iterate through each cell in "Column C" in downward direction.

    I've to fetch values from the corresponding cells for which I'm using vLookup. I need to fetch multiple values so I would be using more vLookups in the loop after I get this working.

    All these fetched values would be clubbed and based on conditions counters ,like one of them being "A" would be updated and summed at last to find the sum of each counter variables.

    Here I'm trying to do is use loop for application "VLookup", but for some reasons it is not working.

    I've tried using
    ..VLookup( Range("C" & i), ...
    ..VLookup("C" & i, ...
    ..VLookup(Cells(i, "C"), ...

    i is the counter and C is the column. Could you please help me with it? I've been searching for help from past 8 hours. Finally I had to paste. I'll attach the file too.


    Sub CalculateApril()
        
    Dim i As Integer, A
    A = 0
    Dim isDecomm
    Dim myRange As Variant, mySelectedArea
    
    mySelectedArea = ThisWorkbook.Worksheets("MSL").Range("C2:G22")
       
        For i = 2 To LastCellInColumn - 1
        With ActiveSheet
        ' Define Range
        myRange = "C" & i
            '   Issued in April. Checks for the Month
            If Range(myRange).Value = 4 Then
                A = A + 1
                '   Are they Decommissioned ? Yes = Ignore, No - Count
                isDecomm = Application.VLookup(Cells(i, "C").Value, Range("C2:G22"), 5, False)
                Debug.Print Cells(i, "C").Value
                'MsgBox isDecomm
            End If
        End With
        '   Increment i/Row
        Next i
       
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Application.VLookup loop not working.

    Hello bgagan911,

    Welcome to the Forum!

    The syntax for VLookup in VBA is different than that for the worksheet formula. For example, your line of code below:
      What you have...
        isDecomm = Application.VLookup(Cells(i, "C").Value, Range("C2:G22"), 5, False)
    
    
      What you should have...
        isDecomm = Application.VLookup(Cells(i, "C").Value, Range("C2:G22").Columns(5), 1, False)

    Additionally, you will need to check if the returned value was an error. Declare the variable that will hold the VLookup result as a Variant. Using the VBA VarType function, check if the result is equal to the constant vbError.


    Here is the macro with the corrections added.
    Sub CalculateApril()
        
      Dim i As Integer, A
      A = 0
      Dim isDecomm
      Dim myRange As Variant, mySelectedArea
    
      mySelectedArea = ThisWorkbook.Worksheets("MSL").Range("C2:G22")
       
        For i = 2 To LastCellInColumn - 1
        With ActiveSheet
        ' Define Range
        myRange = "C" & i
            '   Issued in April. Checks for the Month
            If Range(myRange).Value = 4 Then
                A = A + 1
                '   Are they Decommissioned ? Yes = Ignore, No - Count
                isDecomm = Application.VLookup(Cells(i, "C").Value, Range("C2:G22").Columns(5), 1, False)
                Debug.Print Cells(i, "C").Value
                If VarType(isDecomm) = vbError Then
                   MsgBox "Not found."
                Else
                   MsgBox isDecomm
                End If
            End If
        End With
        '   Increment i/Row
        Next i
       
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-11-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Application.VLookup loop not working.

    Its keeps on saying NOT Found. I'll trying playing around the code and revert it when done

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0