0

Hi I am new to VBA and am having trouble finding the solution to what I'm trying to do.

I have 2 columns

Lvl1 Lvl2
A - - X
A - - X
A - - X
B - - X

If Lvl2 is "X", then I want it to be replaced by the same value as Lvl1. So from first to third row, Lvl2 would be "A", and in row 4 Lvl2 would be "B".

Another way of seeing this woud be if Lvl1 is A, then Lvl2's text would be A as well. If Lvl1 is B, then Lvl2's text would be B, etc.

Jade
  • 77
  • 1
  • 14
  • what have you tried so far? Also, why not just copy column `A` into column `B` -- sounds like that is all you are doing... – cybernetic.nomad Mar 26 '19 at 16:14
  • 2
    Welcome to SO. First of all, even if you are new to VBA, post what code you have tried. Second, *f Lvl1 is A, then Lvl2's text would be A as well. If Lvl1 is B, then Lvl2's text would be B, etc* means column lvl2 is **exactly** the same than column 1, so just copy and paste, or use a formula like `=A1` and dragdown. – Foxfire And Burns And Burns Mar 26 '19 at 16:17

1 Answers1

0

Here is some code that will get you started -

  1. Loop through your rows. Here it is 2 to 4, when implementing, you will want to create a dynamic loop (For i = 2 to LastRow). How to do that is well documented here
  2. Check if the value in Column B = X - If so, give Column B the value from Column A

Dim ws as Worksheet: Set ws = ThisworkBook.Sheets("Sheet1")
Dim i as Long

For i = 2 to 5
    If ws.Range("B" & i) = "X" Then
        ws.Range("B" & i).Value = ws.Range("A" & i).Value
    End If
End if
urdearboy
  • 14,439
  • 5
  • 28
  • 58