Search code examples
excelvbaif-statementoffsetmultiple-conditions

If with two conditions based on two cells


I need to add a second condition to my code.

If Len = 3 and UPC is blank, then move the Group Code left 1 column. I've the action part resolved. The second condition has me stumped.

I tried And in the same line with the two conditions.

I tried what you see below.

The Group codes are in Column C.
The UPCs are column E.
If cells in C, len = 3 AND UPC in same row, column E = "" (blank) then proceed otherwise skip.

I added:

Dim UPC As Range
Set UPC = Range("E2:E" & LastRow)
If UPC.Value = "" Then

Here is the code:

'Moving Group Codes
Dim cell As Range
Dim UPC As Range
Set UPC = Range("E2:E" & LastRow)
For Each cell In Intersect(Range("C:C"), ActiveSheet.UsedRange)
    If Len(cell.Value) = 3 Then
        If UPC.Value = "" Then  '<<<<<<this is where i get my error.
            cell.Cut Destination:=cell.Offset(columnoffset:=-1)
        End If
    End If
Next cell

enter image description here


Solution

  • Try this:

    Dim ws As Worksheet, cell As Range
    Dim UPC As Range
    
    Set ws = ActiveSheet
    Set UPC = ws.Range("E2:E" & LastRow)
    For Each cell In Intersect(ws.Range("C:C"), ws.UsedRange).Cells
        If Len(cell.Value) = 3 Then
            If Len(cell.EntireRow.Columns("E").Value) = 0 Then  '<< check ColE value on same row
                cell.Cut Destination:=cell.Offset(columnoffset:=-1)
            End If
        End If
    Next cell