Search code examples
excelvba

IF statement macro keeps moving cell


I have created a recorded macro where I want an If statement on column E to tell me if there is text in column D. I also want this to go down the column until it reaches the end of data.

Issue is that when I press my button that I have created with button assigned it seems to move and try doing the statement from somewhere else.

I also have 2 other buttons that both work as expected.

' CLEAR Macro
'

'
    Columns("E:G").Select
    Selection.End(xlDown).Select
    Range("E3:G3").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWindow.SmallScroll Down:=-5
    Selection.ClearContents
    Range("G1").Select
End Sub
Sub IFSTATEMENT()
'
' IFSTATEMENT Macro
'

'
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""",""X"",""1"")"
    Range("E3").Select
    Selection.AutoFill Destination:=Range("E3:E42")
    Range("E3:E42").Select
End Sub
Sub VLOOKUP()
'
' VLOOKUP Macro
'

'
    Range("G3").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],C[-3]:C[-2],2,FALSE)"
    Range("G3").Select
    Selection.AutoFill Destination:=Range("G3:G42")
    Range("G3:G42").Select
    Range("G1").Select
End Sub

screenshot after pressed button "Compare (IF)".The number 1 that can be seen in column G should be in E

thank you


Solution

  • Sub macro3()
    
        ' CLEAR Macro
        Columns("E:G").ClearContents
        ' IFSTATEMENT Macro
        Range("E3:E42").FormulaR1C1 = "=IF(RC[-1]="""",""X"",""1"")"
        ' VLOOKUP Macro
        Range("G3:G42").FormulaR1C1 = "=VLOOKUP(RC[-1],C[-3]:C[-2],2,FALSE)"
        
    End Sub