Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

copy cells value from sheet to another

0

 hello 

I  try   desighn macro  to  copy cells value  from  sheet to  another   the  cells  value  in  column a    in  sheet1   contains   empty   cells   among cells value  and    supposes copying  to  sheet2   in  column b   without   empty cells   but  it  doesn't  work 

what  I  want  when  write  cell value  in column a  in sheet1  automatically  copy  to  col b in sheet2 

may be   my  code   doesn't  make  sense , but  I  try   to  learn  how  to  write  code 

I'm a new  begginer  in  vba   ,  so  I try   learning  from  the  internet  

I put  the expected result  in sheet2

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 1 Then
        If Target.Value <> "" Then
            Target.Value = Sheets("sheet2").Columns(2)
        End If
    End If
    End Sub

thanks in advance 

Answer
Discuss

Answers

0
Selected Answer

Hi Speed

Unfortunately, your line 

Target.Value = Sheets("sheet2").Columns(2)

changes the value of the cell you've entered to the value of sheet 2 column B! (Making it blank at present).

If you want any value typed in sheet1 column A to go to sheet2 (even if you are over-typing an existing value in sheet1), this will work:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column = 1 Then

        If Target.Value <> "" Then
            LastCell = Sheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Row
            Sheets("sheet2").Cells(LastCell + 1, 1).Value = Target.Value
        End If

    End If

End Sub

To understand the bit in bold above (which puts the sheet1 value below existing data in sheet 2, see Don's tutorial Get the Last Row using VBA in Excel. To see how to do the same for columns, see Find the Last Column with Data in Excel VBA.

If you're new to VBA,  go to the Tutorials page and find Don's 6 introductory lessons, the first one being Excel Macros Class 1 - Getting Started Programming Macros

Discuss

Discussion

great work !   thanks  for  correct me  , also the  links 
best regards,
Speed
speed (rep: 40) Jan 4, '21 at 5:13 am
Thanks Speed and good luck!
John_Ru (rep: 6142) Jan 4, '21 at 5:17 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login