+ Reply to Thread
Results 1 to 16 of 16

VBA Vlookup returns 1004 error

  1. #1
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    VBA Vlookup returns 1004 error

    I am trying to make a for if statement that returns a value in column b if one of two conditions are met.
    Spreadsheet View.JPG

    If I put in On Error Resume Next it will skip the if condition and still place a value on the next sheet. How do I get it to ignore the condition if there is no ABC or DEF for other lines?

    Sub Code()

    Dim totalrows As Long
    Dim i As Integer

    Sheets("Data").Activate

    totalrows = Sheets("Data").UsedRange.Rows.Count

    i = 1

    For x = 1 To totalrows
    If Sheets("Data").Cells(x, 1).Value = 0 Then
    If WorksheetFunction.VLookup(Sheets("Data").Cells(x, 2) & "ABC", Sheets("Data").Columns("A:I"), 9, False) > 30 Or WorksheetFunction.VLookup(Sheets("Data").Cells(x, 2) & "DEF", Sheets("Data").Columns("A:I"), 9, False) > 30 Then
    Sheets("Notifications").Cells(i, 1) = Sheets("Data").Cells(x, 2)
    i = i + 1
    End If
    End If
    Next x

    Sheets("Notifications").Select

    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Vlookup returns 1004 error

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    My apologies. I just created my account so I am still figuring these things out.

    I am trying to make a for if statement that returns a value in column b if one of two conditions are met.
    Spreadsheet View.JPG

    If I put in On Error Resume Next it will skip the if condition and still place a value on the next sheet. How do I get it to ignore the condition if there is no ABC or DEF for other lines?

    I worked on this some more and got the previous issue to resolve but now the problem is it is still returning value 3 despite both numbers in column I being less than 30

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Vlookup returns 1004 error

    Don't use On Error Resume Next, it can just hide errors.

    That won't help you find what the real problem is.

    If you remove it where does the code error?

    Also, what are the values of a, b and c?

    You can check that in the Locals Window (View>Locals Window)

  5. #5
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    I'm using the On Error Resume Next in order to have VLookup not give me the 1004 error since there is not a matching value all the time.

    I want it to ignore the error for VLookup when there is no matching value and go on to the next VLookup.

    The values of a,b, and c change for each value of x.
    How do i check the values of a, b, and c in the Locals Window?
    Does it show me the value of a,b, and c for each value of x?

  6. #6
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    I'm using the On Error Resume Next in order to have VLookup not give me the 1004 error since there is not a matching value all the time.

    I want it to ignore the error for VLookup when there is no matching value and go on to the next VLookup.

    The values of a,b, and c change for each value of x.
    How do i check the values of a, b, and c in the Locals Window?
    Does it show me the value of a,b, and c for each value of x?

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Vlookup returns 1004 error

    Honestly, don't use On Error Resume Next.

    I don't have a workbook to work with but see if this helps - it should at least stop the runtime errors.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    That did stop the runtime errors but it then only returns values in the next sheet it only returned value 4 since 1 2 and 3 don't have ABC,DEF,and GHI. I want to be able to have it return a value on the next sheet if even if ABC,DEF, and GHI are not all listed under each number.
    The result I am looking for is on the next sheet to have 1,3,4 in the first 3 cells going down column A. This would be because in column I on the first sheet one of the numbers is above 30 and one would be below.

  9. #9
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    That did stop the runtime errors but it then only returns values in the next sheet it only returned value 4 since 1 2 and 3 don't have ABC,DEF,and GHI.

    I want to be able to have it return a value on the next sheet if even if ABC,DEF, and GHI are not all listed under each number.

    The result I am looking for is on the next sheet to have 1.4 in the first 2 cells going down column A.
    This would be because in column I on the first sheet one of the numbers is above 30 and one would be below.
    Last edited by Sportz234; 03-15-2013 at 02:18 PM.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Vlookup returns 1004 error

    If you attach a workbook I can have look at why the result isn't correct.

  11. #11
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    Here is the workbook. I hope I attached it correctly.

    Tool.xlsm

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Vlookup returns 1004 error

    The problem here is when any of the VLookups return an error then the If statements will fail with a runtime error.

    That is caused by trying a comparison between an actual value and an error value.

    I added the If Not(IsError(a) Or ... to avoid that error.

    I've changed the code, and it now gives the result you posted in post #8.
    Please Login or Register  to view this content.
    [/code]

  13. #13
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    My apologies. Since I am still new to this forum. I didn't know how to get rid of one of my replies. The result I want is the one stated in #9 not #8.
    I'm sorry for the misunderstanding. Would you be able to tell me how to get rid of replies I have made so if I make one of the mistakes shown above like posting twice or having a typo that I can fix it so it doesn't cause misunderstandings

  14. #14
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    I got it thanks to your code. I just had to change the if error result to 30 so that it doesn't prove either of my statements true.
    Thank You for all your help. It is much appreciated.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Vlookup returns 1004 error

    I saw post #9 but thougt it was a type because you said you wanted the result of 1.4 in the first 2 cells, and I was wondering where the 1.4 came from as all the values are integers.

  16. #16
    Registered User
    Join Date
    03-15-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Vlookup returns 1004 error

    This is the code that worked:
    Please Login or Register  to view this content.

+ 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.6.0 RC 1