Home > Software design >  Is there an easier way to write this IF statement?
Is there an easier way to write this IF statement?

Time:11-01

I'm very new to VBA and trying to learn as I go along. I'm 100% sure there is an easier way to condense this code rather than typing If statements for each cell and corresponding cell.

Here is the code I'm using:

Sub Test()
    If Range("B1").Value > 0 Then
        Range("A1").Value = 0
    End If
    
    If Range("B2").Value > 0 Then
        Range("A2").Value = 0
    End If
    
    If Range("B3").Value > 0 Then
        Range("A3").Value = 0
    End If
    
    If Range("B4").Value > 0 Then
        Range("A4").Value = 0
    End If
    
    If Range("B5").Value > 0 Then
        Range("A5").Value = 0
    End If
    
    If Range("B6").Value > 0 Then
        Range("A6").Value = 0
    End If
End Sub

Picture

The range I'm using is A1:A6 then comparing that against values of B1:B6 and saying if B1 > 0 then A1 = 0, then repeat for A2 and B2 and so on.

I know that the solution is probably painfully obvious, but I can't seem to figure out how to do it yet.

Thanks for any help provided!

CodePudding user response:

You can either

  • use a formula evaluation in VBA based on a simple Excel formula
    =IF(B1:B6,A1:A6,0)

to replace the original data in column A or

  • write the mentioned formula directly into e.g. C1 benefitting from the newer dynamic features of version 2019 /MS 365 displaying it as so called spill range (otherwise you need to enter it as array formula confirming it via Ctrl Shift Enter).

Example call in VBA

Option Explicit                         ' head of code module (force declaration of variables and objects)

Sub Example()
    Dim ws As Worksheet
    Set ws = Sheet1                     ' << change to your project's sheet Code(Name)
    Dim data As Variant                 ' provide for a 1-based 2-dim datafield array
    'assign worksheet related evaluation to data
    data = ws.Evaluate("=If(B1:B6,A1:A6,0)")
    'write to target
    ws.Range("A1").Resize(UBound(data), 1).Value = data
End Sub

CodePudding user response:

Simplify Multiple If Statements

Microsoft Docs

The Code

  • Both procedures do the same. Due to using constants, the first solution can be quickly adapted by modifying values in one place (at the beginning of the code).
Option Explicit

Sub ForEachNext()
    
    Const sfCellAddress As String = "B1"
    Const srCount As Long = 6
    
    Const dCol As String = "A"
    
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    Dim sCell As Range
    Dim sValue As Variant
    Dim dCell As Range
    
    For Each sCell In ws.Range(sfCellAddress).Resize(srCount).Cells
        sValue = sCell.Value
        If IsNumeric(sValue) Then ' is a number
            If sValue > 0 Then ' is greater than
                Set dCell = sCell.EntireRow.Columns(dCol)
                dCell.Value = 0
            'Else ' is less than or equal ('sValue <= 0')
            End If
        'Else ' is not a number
        End If
    Next sCell

End Sub

Sub ForEachNextMagicNumbers()
    
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    Dim sCell As Range
    Dim sValue As Variant
    Dim dCell As Range
    
    For Each sCell In ws.Range("B1").Resize(6).Cells
        sValue = sCell.Value
        If IsNumeric(sValue) Then ' is a number
            If sValue > 0 Then ' is greater than
                Set dCell = sCell.EntireRow.Columns("A")
                dCell.Value = 0
            'Else ' is less than or equal ('sValue <= 0')
            End If
        'Else ' is not a number
        End If
    Next sCell

End Sub
  • Related