Lynn Holland
Lynn Holland

Reputation: 11

VBA IF condition

Please help, I am new and I'm trying to do an If condition. Here is my code but I need -

UnitUsed.Value = If Description.Value=Cells(1,7).Value = "Ton" Then Acres.Value=Cells(1,5).Value * Rate.Value=Cells(1,6).Value/2000 Else Acres.Value=Cells(1,5).Value * Rate.Value=Cells(1,6)

I'm not sure what I'm doing wrong in writing the If condition.

Private Sub PopulateForm(SelectedRow As Range)

        With SelectedRow
    
        Calendar1.Value = .Cells(1, 1).Value
        CropFieldName.Value = .Cells(1, 2).Value
        CropType.Value = .Cells(1, 3).Value
        Description.Value = .Cells(1, 7).Value
        Product.Value = .Cells(1, 4).Value
        Acres.Value = .Cells(1, 5).Value
        Rate.Value = .Cells(1, 6).Value
        Price.Value = .Cells(1, 9).Value
        UnitUsed.Value = .Cells(1, 8).Value
        
    End With
End Sub

Upvotes: 0

Views: 61

Answers (1)

Jortx
Jortx

Reputation: 727

What do you need would be something like this?

In several lines:

If description.Value = "Ton" Then
    Acres.Value = Acres.Value * Rate.Value / 2000
Else
    Acres.Value = Acres.Value * Rate.Value
End If

In one line:

Acres.Value = IIF(description.Value = "Ton", Acres.Value * Rate.Value / 2000, Acres.Value * Rate.Value)

Upvotes: 2

Related Questions