1

I'm new to VBA and trying to resolve this error regarding the IF statement. I'm trying to change my integer variable to 3 depending on my Activesheet cell value which is currently typed FALSE.

Sub IFStatement() 
Dim w as integer 

w = 0  
If Activesheet.cells(58, 11).value = "FALSE" 
Then   w = 3
End If 
MsgBox w
End sub

Thank you!

1
  • 2
    Try .value = False as you want to compare against a boolean value not a string containing the word False. Commented Feb 4, 2022 at 12:45

1 Answer 1

2

The If Statement

If...Then...Else statement

Option Explicit

Sub IFStatement()
    
    Dim w As Long
    ' w = 0 ' is redundant because its initial value is 0 (when declared).
    
    ' 1 line
    
    ' If it is a boolean:
    If ActiveSheet.Cells(58, 11).Value = False Then w = 3
    'If CStr(ActiveSheet.Cells(58, 11).Value) = "FALSE" Then w = 3
    ' Case-insensitive
    'If StrComp(CStr(ActiveSheet.Cells(58, 11).Value), "FaLSe", vbTextCompare) = 0 Then w = 3
    
    ' If it is a string ('FALSE):
    'If ActiveSheet.Cells(58, 11).Value = "FALSE" Then w = 3
    ' Case-insensitive
    'If StrComp(ActiveSheet.Cells(58, 11).Value, "FaLSe", vbTextCompare) = 0 Then w = 3
    
     
    ' 3 lines Another way to write the If statement:

'    If ActiveSheet.Cells(58, 11).Value = False Then ' finishes with 'Then'
'        w = 3
'    End If
    
    MsgBox w

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

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.