+ Reply to Thread
Results 1 to 5 of 5

VBA Error Handler: place zero in error cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-10-2015
    Location
    the Dysfunctional Empire of Bulgaria
    MS-Off Ver
    2010
    Posts
    102

    VBA Error Handler: place zero in error cell

    How do I adjust the code below to place a zero in cells excel calculates as errors?

        k = 2
        On Error GoTo ERROR_HANDLE
        Do Until IsEmpty(Cells(k, 2))
            Cells(k, 11).Value = Application.WorksheetFunction.VLookup(Cells(k, 5), THE_RANGE, 14, False)
        k = k + 1
        Loop
        Exit Sub
    ERROR_HANDLE: ???

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Error Handler: place zero in error cell

    I believe (could be wrong) the way you have the code written only one cell will get a value of zero. If you are wanting all the error cells with a zero you will need to amend your code. Here is the answer to your initial question

    k = 2
        On Error GoTo ERROR_HANDLE
        Do Until IsEmpty(Cells(k, 2))
            Cells(k, 11).Value = Application.WorksheetFunction.VLookup(Cells(k, 5), THE_RANGE, 14, False)
        k = k + 1
        Loop
        Exit Sub
    ERROR_HANDLE:
    Cells(k, 11).Value = 0
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Forum Contributor
    Join Date
    10-10-2015
    Location
    the Dysfunctional Empire of Bulgaria
    MS-Off Ver
    2010
    Posts
    102

    Re: VBA Error Handler: place zero in error cell

    You are absolutely right, I do want a zero in all error cells.

    How should the code go?

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Error Handler: place zero in error cell

    Maybe this:

    k = 2
    
        Do Until IsEmpty(Cells(k, 2))
            If IsError(Application.WorksheetFunction.VLookup(Cells(k, 5), THE_RANGE, 14, False)) Then
                Cells(k, 11).Value = 0
            Else
                Cells(k, 11).Value = Application.WorksheetFunction.VLookup(Cells(k, 5), THE_RANGE, 14, False)
            End If
        k = k + 1
        Loop
        Exit Sub

  5. #5
    Forum Contributor
    Join Date
    10-10-2015
    Location
    the Dysfunctional Empire of Bulgaria
    MS-Off Ver
    2010
    Posts
    102

    Re: VBA Error Handler: place zero in error cell

    Thanks stnkynts.

    Here is another solution I came up with.

    k = 2
     On Error Resume Next
     Do Until IsEmpty(Cells(k, 2))
        
            Cells(k, 11).Value = Application.WorksheetFunction.VLookup(Cells(k, 5), THE_RANGE, 14, False)
            
            If IsEmpty(Cells(k, 11).Value) Then Cells(k, 11).Value = "0" Else Cells(k, 11).Value = Cells(k, 11).Value
            Next
         
    k = k + 1
    Loop
    Last edited by lostest; 11-26-2015 at 06:10 PM. Reason: spelling

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. 'While' function inside of error handler giving an error/failing
    By dnwadams in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2015, 12:18 AM
  2. [SOLVED] Need error handler code to avoid pivot table refreshing error if no data is available
    By adelkam in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-04-2015, 12:37 PM
  3. Getting Run-time Error 91. Error handler not working.
    By rbs123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-13-2014, 04:20 AM
  4. Error Handler Runs Without Error
    By BigBas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-03-2008, 05:05 PM
  5. Error Handler for Run-time error '9'
    By josnah in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-07-2006, 01:17 PM
  6. Replies: 1
    Last Post: 02-08-2006, 06:25 AM
  7. [SOLVED] Error Handler and Error 1004
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-18-2005, 10:06 AM

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