Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88054

VBA Loop And Return Value If Two Conditions Met

$
0
0

Добро Дан,

I have a For loop that is trying to see if an item ID and quantity matches the item ID and quantity in another worksheet. If those criteria are met, then I want to return the value in one column to the cell this loop was being ran on.

Variables: wsUS As WorksheetwsTHEM As WorksheetLastRowUS As Long

Assuming the variables are properly defined and set, here is my demo Code:

'Starts at 2nd row due to header
For i = 2 To LastRowUS
   'First If matches Item IDs
   If wsUS.Range("F"& i) = wsTHEM.Range("E"& i) Then
      'Second If sees if other worksheet returned "Match item quantities
      If wsTHEM.Range("Y"& i) = "Match" Then
         'Return the value if column X in wsTHEM to column Y in wsUS if ID and quantities matched
         wsUS.Range("Y"& i).Value = wsTHEM.Range("X"& i).Value
      Else
         'Return text
         wsUS.Range("Y"& i).Value = "Discrepancy"
      End If
   End If
Next i

As of now, it seems this loop is working for some rows, but does anyone have any VBA tips with Index/Match or this loop that can help me return the value in Column X of wsTHEM if both criteria are met for ID & Quantity?

Thanks for your help and Happy Halloween


Viewing all articles
Browse latest Browse all 88054

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>