R
ryguy7272
Quick question for you Excel-Looping-Experts out there…
Today I was working with a relatively simple Do Until…Loop. I got stuck, so
I turned on the Macro Recorded and got a couple of lines of code from that.
My code is below…
Sub Compare_AB()
Dim i, j As Integer
Range("A1").Select
i = ActiveCell.Value
Range("B1").Select
j = ActiveCell.Value
Do Until ActiveCell = ""
If i = j Then
Selection.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=RC[-2],""Yes"",""No"")"
End If
Selection.Offset(1, 0).Select
Selection.Offset(0, -2).Select
Loop
End Sub
Yes, I know this is not very eloquent, but it actually works exactly like I
want it to (basically, it compares values in Column A with values in Column
B; if the two match, a “Yes†is placed in Column D, but if they don’t match a
“No†is placed in column D). Now, my question is two fold:
1) How can I make the code more efficient (i.e., get rid of the
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=RC[-2],""Yes"",""No"")" and
Selection.Offset(1, 0).Select and Selection.Offset(0, -2).Select)?
2) The code below simply places a “Yes†in Column D, seemingly without
evaluating the values in Column A and Column B. Why does the code below not
evaluate all of the elements in Column A and Column B?
Sub Compare_AB()
Dim i, j As Integer
Range("A1").Select
i = ActiveCell.Value
Range("B1").Select
j = ActiveCell.Value
Do Until ActiveCell = ""
If i = j Then
Selection.Offset(0, 2).Select
ActiveCell.Value = "Yes"
If i <> j Then
ActiveCell.Value = "No"
End If
End If
Selection.Offset(1, 0).Select
Selection.Offset(0, -2).Select
Loop
End Sub
Thanks for your time and effort!!
Today I was working with a relatively simple Do Until…Loop. I got stuck, so
I turned on the Macro Recorded and got a couple of lines of code from that.
My code is below…
Sub Compare_AB()
Dim i, j As Integer
Range("A1").Select
i = ActiveCell.Value
Range("B1").Select
j = ActiveCell.Value
Do Until ActiveCell = ""
If i = j Then
Selection.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=RC[-2],""Yes"",""No"")"
End If
Selection.Offset(1, 0).Select
Selection.Offset(0, -2).Select
Loop
End Sub
Yes, I know this is not very eloquent, but it actually works exactly like I
want it to (basically, it compares values in Column A with values in Column
B; if the two match, a “Yes†is placed in Column D, but if they don’t match a
“No†is placed in column D). Now, my question is two fold:
1) How can I make the code more efficient (i.e., get rid of the
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=RC[-2],""Yes"",""No"")" and
Selection.Offset(1, 0).Select and Selection.Offset(0, -2).Select)?
2) The code below simply places a “Yes†in Column D, seemingly without
evaluating the values in Column A and Column B. Why does the code below not
evaluate all of the elements in Column A and Column B?
Sub Compare_AB()
Dim i, j As Integer
Range("A1").Select
i = ActiveCell.Value
Range("B1").Select
j = ActiveCell.Value
Do Until ActiveCell = ""
If i = j Then
Selection.Offset(0, 2).Select
ActiveCell.Value = "Yes"
If i <> j Then
ActiveCell.Value = "No"
End If
End If
Selection.Offset(1, 0).Select
Selection.Offset(0, -2).Select
Loop
End Sub
Thanks for your time and effort!!