VBAnoob
VBAnoob

Reputation: 163

Excel VBA "If" statement Error

So I'm getting an error with some code and I don't know how to get around it

Range("K" & varOffset).Select
           Output = If (ISBLANK(H2), "No", "Yes")      <----------Shows up red
           ActiveCell.FormulaR1C1 = Output`              

How do I make this if statement work?

Upvotes: 0

Views: 787

Answers (3)

99moorem
99moorem

Reputation: 1983

the below will avoid the need to select a cell which should always be avoided. It also avoids the need for the output variable

with Range("K" & varOffset)
    If trim(Range("K" & varOffset).offset(0,-2).value)="" then'If blank
        .value= "Yes"
    else
        .value= "No"
    endif
end with

Upvotes: 0

yoyoyoyo123
yoyoyoyo123

Reputation: 2472

Looks like you are trying to use the worksheet version of IF.

Try VBA IF block

If IsEmpty(H2) Then
    Output = "No"
Else
    Output = "Yes"
End If

Upvotes: 1

Krishna
Krishna

Reputation: 2481

Try this

Formulae in Excel is not VBA. VBA has its code structure that needs to be adherered to

Range("K" & varOffset).Select
If trim(Range("H2")) = "" then 
  output = "No"
else 
  output =  "Yes"
end if
ActiveCell.FormulaR1C1 = Output 

Upvotes: 0

Related Questions