0

I am adding a button in my excel sheet for adding an employee with above used formulas.

First two formulas in the below code work perfect but last one giving syntax error.

My code is :

Private Sub AddEmployee_Click()

    Sheets("Sheet1").Range("A35").Select
    ActiveCell.EntireRow.Insert shift:=xlDown

    Sheets("Sheet1").Range("A35:AJ35").Select
    Selection.Borders.Weight = xlThin

    Sheets("Sheet1").Range("AJ35").Select
    ActiveCell.Formula = "=SpeDays(C35:AG35,9)"

    Sheets("Sheet1").Range("AI35").Select
    ActiveCell.Formula = "=IF(AI35>=10,10,AI35)"

    Sheets("Sheet1").Range("AH35").Select
    ActiveCell.Formula = "=COUNTIF(C35:AG35,">=4000")"
2
  • Is your error only in the last one? Commented Jul 24, 2017 at 8:01
  • On a side note: you can remove the select and activecell and join the two lines - Sheets("Sheet1").Range("A35").EntireRow.Insert shift:=xlDown Commented Jul 24, 2017 at 8:07

2 Answers 2

3

The problem are your quotes inside the formula. Use double quotes as a solution:

ActiveCell.Formula = "=COUNTIF(C35:AG35, "">=4000"")"
Sign up to request clarification or add additional context in comments.

3 Comments

but excel file return formula like this : =IF(AI36>=10,10,AH36)
it is showing application defined or object defined error
IT works brother. I have made some mistakes, now identified. thanks
1

Use can use the below code as well

Activecell.Formula = "=Countif(C35:AG35," & chr(34) & ">=4000" & chr(34) & ")" 

Output will be:

=Countif(C35:AG35,">=4000")

5 Comments

it is showing application defined or object defined error
@PratyushKumar - There's typo, changeAG3,5 to AG35,
Is there any way to apply the same code for last row determined automatically in place of A35 OR AJ35....
LastRow = ActiveSheet.Cells(.Rows.Count, "A").End(xlUp).Row + 1 For this, you need to have a fixed row, either its in A coloumn or AJ column. Thanks
@nishit dey- for this i have to put only $ with A or AJ. am i right

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.