Sub test()
Dim score As Integer, result As String
score = Range("A1").Value
If score > 0 Then result = "pass"
Range("b1").Value = result
End Sub
I'm missing something here. In cell A1 i have 0.04 which is > 0 But in B1 I'm not returning pass
Sub test()
Dim score As Integer, result As String
score = Range("A1").Value
If score > 0 Then result = "pass"
Range("b1").Value = result
End Sub
I'm missing something here. In cell A1 i have 0.04 which is > 0 But in B1 I'm not returning pass
If you are new to VBA, it may be better to reference cells and their .Value
property, which is a Variant
, and is designed to handle values of multiple types more intuitively:
Sub test()
Dim score As Range: Set score = Range("A1")
Dim result As Range: Set result = Range("B1")
If 0 < score.Value Then
result.Value = "pass"
' Else
' result.Value = "fail"
End If
End Sub
Score
as I mentioned in your comments.Sub PopulateConditionally()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
' Could be anything (number, string, boolean, error) so use a 'Variant' type.
Dim CellValue As Variant: CellValue = ws.Range("A1").Value
Dim Score As Double, Result As String
If VarType(CellValue) = vbDouble Then ' is a number
Score = CDbl(CellValue) ' explicit conversion (you're in control)
'Or:
'Score = CellValue ' implicit conversion (VBA is in control)
If Score > 0 Then
Result = "pass"
'Else ' <=0; do nothing; 'Result' remains an empty string ("")
End If
'Else ' is not a number; do nothing; 'Result' remains an empty string ("")
End If
ws.Range("B1").Value = Result
End Sub
For this line "Dim score As Integer" you will have to change data type to variant or double. As its integer 0.04 will be stored as 0 in the variable