Copy value after If

uniks

New Member
Joined
Jan 6, 2015
Messages
19
Hi everyone
I have number 1 to 10 in Column A and <code>name</code> in Column B. What I am trying to accomplish is to check the <code>name</code> in cell B2, if empty then it will copy the number from cell A2 to cell H2. But if cell B2 already contains a name then it will copy the number from cell A3 to cell H2 and so on.
here is my code
Code:
<code>Private Sub CommandButton1_Click()
If IsEmpty(Range("B2")) Then
    Range("A2").Copy destination:=Range("H2")
Else
    Worksheets("sheet6").Cells(Rows.Count, 1).End(xlUp).Offset(0, 1).Copy _
    destination:=Range("H2")
End If
End Sub</code>
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
what you describe is

Code:
If IsEmpty(Range("B2")) Then
    Range("H2").Value = Range("A2").Value
Else
    Range("H2").Value = Range("A3").Value
    
End If

But what i think you mean is

Code:
If IsEmpty(Cells(ActiveCell.Row, "B")) Then
    Cells(ActiveCell.Row, "H").Value = Cells(ActiveCell.Row, "A")
Else
    Cells(ActiveCell.Row, "H").Value = Cells(ActiveCell.Row + 1, "A")
   
End If
 
Upvote 0
your code didn't work like what i've expected
what i want is to check the cell B2 whether it's empty or not. IF it's EMPTY
 
Upvote 0
sorry for the incomplete post above,
what i want is to check the cell B2 whether it's empty or not. IF it's EMPTY then cell B2 will filled by value "A", then copy the value from A2 and place it in H2. After that check the cell B3 whether it's empty or not. IF it's EMPTY then cell B3 will filled by value "A", then copy the value from A3 and place it in H2.
 
Upvote 0
Something More like

Code:
'MyDepth = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
For Each MyCell In Range("B2:B3")
    If IsEmpty(MyCell) Then
        MyCell.Value = "A"
        Range("H2").Value = Cells(MyCell.Row, "A").Value
    End If
Next MyCell
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,979
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top