0

I am playing around with VBA in Excel, and cant for the life of me figure out why this error is popping up.

The following code works:

Cells(1, 1) = " =if(counta( " & aLoc & "Subbies'!$A$2:$A$20>0 ""YES"", ""NO"" "

The following code doesnt work:

Cells(1, 1) = "=if(counta( " & aLoc & "Subbies'!$A$2:$A$20>0 ""YES"", ""NO"" "

So Just by removing the space to make the line of code insert a formula rather than a string causes the compiler to through up error 1004.

In other lines ive used itll insert a formula no problem, but concatenating text with a local variable seems to throw this up. Any ideas?

14
  • It is because the formula is not complete. You are missing all your ) and so excel cannot put it in as it is a bad formula. Commented Jan 6, 2017 at 14:20
  • I dont really care if the formula works at the moment (im just messing around at the moment) but the error isnt within excel, its within the code, ie the compiler wont allow the string to be inserted, even though its wrong. It should be an excel error, not a VBA error Commented Jan 6, 2017 at 14:21
  • I should clarify; the formula was complete when I started it all, and then started removing things to see if it was those characters throwing up the error, until I found it was the = sign touching the " without a space. Commented Jan 6, 2017 at 14:22
  • That is correct the compiler will not allow a formula that is incorrect to be entered. If you tried to enter this directly into excel an error would occur vba is throwing that error. Commented Jan 6, 2017 at 14:23
  • Cells(1, 1) = "=if(counta('" & aLoc & "Subbies'!$A$2:$A$20)>0, ""YES"", ""NO"" )" Commented Jan 6, 2017 at 14:24

2 Answers 2

4

Excel will not allow an incorrect formula to be entered.

When putting a formula directly in the sheet you get the an error if it is not a correct formula and the cell remains in edit mode till either deleted or fixed.

The 1004 error is just vba representing that error.

To help find the error in the formula, place your string in a variable then debug.print it to see what is not correct:

t = "=if(counta('" & aLoc & "Subbies'!$A$2:$A$20)>0, ""YES"", ""NO"" )"
Debug.Print t 

This will put the formula in the immediate window as it would in excel, allowing the developer to check to see if there is an error.

Bottom line:

You can put a string of what ever you want in a cell, but if you are trying to put a formula it must be complete and accurate.

Sign up to request clarification or add additional context in comments.

Comments

0

Excel is complaining that you are trying to put a bad formula in a cell. If you want to put a "formula as text" in a cell, then:

Sub DontQuoteMe()
    Cells(1, 1) = "'=if(counta( " & aLoc & "Subbies'!$A$2:$A$20>0 ""YES"", ""NO"" "
End Sub

enter image description here

(using the space accomplishes the same thing)

Comments

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.