Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

VBA Sub's

  • 06-06-2012 03:19PM
    #1
    Registered Users, Registered Users 2 Posts: 13,746 ✭✭✭✭


    I am trying to compile a macro that does the following:

    Takes two values - uses a formula and puts the result in a specific cell
    Using this result - uses another formula and puts that result in a specific cell

    I am getting those #DIV/0! errors because my macro is just putting the formula in and it's staying there.
    I want to remove the errors but I am getting a 1400 error when I run it.

    Can someone have a look at the code and advise? I think it has to do with the two subs but I don't know how to take them out and still do the same job. Have tried exit sub, no end sub and two at the end etc

    Sub Triangle_2()
    With Range("L50")
    .Formula = "=G45/G46"
    End With
    
    With Range("J42")
    .Formula = "=J41/L50"
    End With
    End Sub
    
    Sub DeleteErrors()
    
    ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, 16).ClearContents
    
    
    End Sub
    


Comments

  • Moderators, Politics Moderators, Paid Member Posts: 44,043 Mod ✭✭✭✭Seth Brundle


    1. The first sub works fine for me (it does need values in cells G45, G46 and J41)
    2. change the following line:
    ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, 16).ClearContents
    to ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas).ClearContents
    3. Your options in terms of not having divide by zero errors is to use an if statement to see if the divisor is zero:
    Sub Triangle_2()
        With Range("L50")
        .Formula = "=IF(G46=0,0,G45/G46)"
        End With
        
        With Range("J42")
        .Formula = "=IF(L50=0,0,J41/L50)"
        End With
    End Sub
    
    Sub DeleteErrors()
    
        ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas).ClearContents
    
    End Sub
    


Advertisement