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 data to the bottom after data already exist infrequentl

0

Hi

I seek for  way  to  avoid  copy data repeatedly to  the  bottom after data have  alredy  existed . for  instance  if  I  have  data in sheet TABLE 1  up  to  row 55 , then will copy  after this  row  and  start  from row 56  . the  code  does that  but  the  problem  will repeat copying   to  the  bottom  when  run  the  macro  every  time  . so  what  I  want  clearing  data after  last  row  contains data have  already  existed . the  lastrow contains data will change  row 55  or  66 or 44  so  should copy  after  the  lastrow contains data without  copy  to  the  bottom repeatedly  . just  replace  after   lastrow contains data .

Sub copyItems()
  Dim lRow1 As Long, lRow2 As Long
  Dim Purchase As Worksheet
    Set Purchase = Worksheets("PURCHASE")
    lRow1 = Worksheets("Table 1").Cells(Rows.Count, 1).End(xlUp).Row
    lRow2 = Purchase.Cells(Rows.Count, 1).End(xlUp).Row
    With Worksheets("Table 1")
      For i = 2 To lRow2
        .Cells(lRow1 + i - 1, 1).Value = .Cells(lRow1 + i - 2, 1).Value + 1
        .Cells(lRow1 + i - 1, 2).Value = Purchase.Cells(i, 2).Value
        .Cells(lRow1 + i - 1, 3).Value = Purchase.Cells(i, 3).Value
      Next
    End With
End Sub

thanks 

Answer
Discuss

Discussion

Hi Leopard. I'm struggling to understand what you want here. I suspect you don't want to duplicate data which is already in worksheet Table1. Please attach a file and give an example of how data should copy (or not) from worksheet Purchase
John_Ru (rep: 6152) Dec 27, '22 at 5:44 pm
Hi John ,
you  can see the  sheet  TABLE 1 for the  lastrow contains  data  in row 56 and  what  I  look  for  copy  the  whole  data from sheet PURCHASE to sheet TABLE1   under  the  lastrow  contains  data   in row 56  should  start  from row57 .  the  problem is  when  run the macro more  than one  time  will  repeat  copying  to  the  bottom  as  in  sheet  third  after  run  about  three times . but  what  I  want as  in  fourth  sheet  shouldn't  repeat copying to  the  bottom . I  would  clear  cells after lastrow contains data in sheet TABLE1   before  copying data from sheet PURCHASE  without  any  repeating  copy ( should  copying  the  whole  data even  there are duplicates items) the  sheet TABLE 1    data will  change in   location of  lastrow contains  data every  time  and  the   same  thing    the  data in sheet  PURCHASE  will change every  time .so  when  change  data in sheet PURCHASE  will also  data  change  in sheet TABLE 1,  then  could  be  updating  in sheet TABLE 1   without  copying repeatedly  to  the  bottom .
attached file
leopard (rep: 88) Dec 28, '22 at 4:09 am
Add to Discussion

Answers

0
Selected Answer

Leopard

In the modified code below and file attached, I saved a new (static) variable lRowLast which allows the "existing data" added to worksheet "Table 1" to be cleared before data is copied from your worksheet PURCHASE

Rightly or wrongly. I also assumed that the Quantity from worksheet PURCHASE should be in column D (and changed the copy lines to suit, changes in bold below).

Sub copyItems()
  Dim lRow1 As Long, lRow2 As Long
  Dim Purchase As Worksheet
  Static lRowLast As Long ' save last row in Table 1

    With Worksheets("Table 1")
        If lRowLast = 0 Then
            ' if lRowLast not set, calculate it
            lRowLast = .Cells(.Rows.Count, 1).End(xlUp).Row
            Else
            ' if set, clear data from last row down
            .Range(.Cells(lRowLast + 1, 1), .Cells(.Cells(Rows.Count, 1).End(xlUp).Row, 4)).ClearContents
        End If
    End With

    Set Purchase = Worksheets("PURCHASE")
    lRow2 = Purchase.Cells(Rows.Count, 1).End(xlUp).Row

    With Worksheets("Table 1")
      lRow1 = .Cells(.Rows.Count, 1).End(xlUp).Row
      For i = 2 To lRow2
        .Cells(lRow1 + i - 1, 1).Value = .Cells(lRow1 + i - 2, 1).Value + 1
        .Cells(lRow1 + i - 1, 2).Value = Purchase.Cells(i, 2).Value
        'copy Quantity to column D
        .Cells(lRow1 + i - 1, 4).Value = Purchase.Cells(i, 3).Value
      Next
    End With

End Sub

This prevents the repeat copying you talked about and allows modified data from PURCHASE to be copied to Table 1. It does not solve the problem of changes made directly to Table 1 (where you'd need to work out if rows have been deleted before lRowLast). You'd need to think carefully about the possibility of that.

Hope this helps.

Discuss

Discussion

John,
should be in column D (and changed the copy lines to suit, changes in bold below).
should be  column C   not  D .
It does not solve the problem of changes made directly to Table 1 (where you'd need to work out if rows have been deleted before lRowLast). You'd need to think carefully about the possibility of that.
I'm not  sure  what  you  mean, but  I  tried   to  delete  some rows , seem  the  macro  deal with changes in  sheet TABLE 1  it  will copy  under the  lastrow contains  data regardless  where  finish  the  last row 
thanks  very  much .
leopard (rep: 88) Dec 28, '22 at 7:27 am
Thanks for selecting my answer, Leopard.

The problem comes if you add new rows in your original data. The macro will then delete some old original rows before copying from PURCHASE 
John_Ru (rep: 6152) Dec 28, '22 at 7:40 am
umm ! yes  you're   right 
I will search for this problem  how  I can fix  it.
leopard (rep: 88) Dec 28, '22 at 8:14 am
You might use the Worksheet_Change event and see if Target is in a row before lRowLast but it might be tricky seeing if rows were deleted. Or use a different font or colour for the "fixed data" and determine where that ends before deleting data added from PURCHASE. Have a think! 
John_Ru (rep: 6152) Dec 28, '22 at 9:35 am
Add to Discussion


Answer the Question

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