4

I am trying to put the following code into VBA. What I ideally want is to apply the formula to the column H when column A is empty. Any help would be appreciated. endPosition is a variable which holds the row# value where column A is empty.

ActiveCell.Formula = "=SUM(N(FREQUENCY(R[endPosition]C[-1]:R[endPosition]C[-1],R[endPosition]C[-1]:R[endPosition]C[-1])>0))"

This formula is applied at the end when the loop sees a blank cell:

    Range("A1").Select
    beginPosition = 2 'Start from row number 2
    Do While IsEmpty(ActiveCell.Offset(1, 0)) = False
    ActiveCell.Offset(1, 0).Select
    endPosition = ActiveCell.Row
    Loop
    endPosition = endPosition + 1
    Range("H15").Select
    ActiveCell.Formula = "=SUM(N(FREQUENCY(R[endPosition]C[-1]:R[endPosition]C[-1],R[endPosition]C[-1]:R[endPosition]C[-1])>0))"
4
  • I know that this is not the right way. But I am trying to learn Excel and hence the stupid formula. Commented Mar 27, 2012 at 14:52
  • Please post the full error message. Thank you. Commented Mar 27, 2012 at 14:52
  • The error that I get is "Run-time error '1004': Application-defined or object-defined error" Commented Mar 27, 2012 at 14:53
  • When exactly is this line of code called? Commented Mar 27, 2012 at 14:57

1 Answer 1

6

Assuming the formula is correct, if endPosition is a variable, you should not include it in the string:

ActiveCell.FormulaR1C1 = "=SUM(N(FREQUENCY(R[" & endPosition & _
     "]C[-1]:R[" & endPosition & "]C[-1],R[" & endPosition & _
     "]C[-1]:R[" & endPosition & "]C[-1])>0))"
Sign up to request clarification or add additional context in comments.

1 Comment

That resolved the error. I will have the fix the positions for this formula to work correctly. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.