Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VBA Sub's

Options
  • 06-06-2012 3:19pm
    #1
    Registered Users 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 Posts: 39,589 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