VBA - Looking For Best Way(s) To Copy Data To Another Sheet

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I have this code that I am trying to use to copy some data from one sheet to another. It is copying though yet the final output is not something that I am happy with.
At the time of copying, "ITEM 6" will be the below all the other items on the data1 sheet – I am sorting with the items column (col C).
After the copy, only the last "ITEM 6" member from data1 sheet appears for all the "ITEM 6" records.
Is there a problem with this code and or how best can I copy the said data to the data2 sheet?
Thanks in advance.

Code:
Dim e&, ls&, lr&, MyCells As Range
Dim T1 As Worksheet, S1 As Worksheet

Set T1 = Sheets(“Data1”)
Set S1 = Sheets(“Data2”)

e = 0
lr = T1.Range("A" & Rows.Count).End(xlUp).Row
If lr < 7 Then lr = 7

For Each MyCells In T1.Range("C7:C" & lr)
   If MyCells = "ITEM 6" Then
      T1.Range("A" & lr & ":U" & lr).Copy S1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
      T1.Range("A" & MyCells.Row & ":U" & MyCells.Row).ClearContents
      ls = S1.Range("A" & Rows.Count).End(xlUp).Row
      If ls < 7 Then ls = 7

      With S1.Range("A7:A" & ls)
         Do
            e = e + 1
         Loop Until IsError(Application.Match(e, S1.Cells, 0))
     End With
     S1.Cells(ls, "A") = e
  End If
Next MyCells
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I see that

T1.Range("A" & lr & ":U" & lr).Copy S1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
T1.Range("A" & MyCells.Row & ":U" & MyCells.Row).ClearContents

you are copying same Range("A" & lr) each time MyCells= "ITEM 6" and then Clear Range("A" & MyCells.Row)? Why is that?
 
Upvote 0
I see that

T1.Range("A" & lr & ":U" & lr).Copy S1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
T1.Range("A" & MyCells.Row & ":U" & MyCells.Row).ClearContents

you are copying same Range("A" & lr) each time MyCells= "ITEM 6" and then Clear Range("A" & MyCells.Row)? Why is that?


I am clearing that row (range) because I don't want to have those records on the data1 sheet after copying.

It's like moving the range of data from the data1 sheet to the data2 sheet.

I am not familiar with the "move" syntax and I don't want to interfere with my sheet formating.

Like I said before, I am looking for a fix and a better way to handle my problem.
 
Upvote 0
Hard to visualize. Can you copy or upload sample data?
 
Upvote 0
Not sure what the blue segment of the code does, but your problem is probably because you are only copying the last row with lr. Change it to reference MyCells.Row and it should work fine.

VBA Code:
Dim e&, ls&, lr&, MyCells As Range
Dim T1 As Worksheet, S1 As Worksheet

Set T1 = Sheets(“Data1”)
Set S1 = Sheets(“Data2”)

e = 0
lr = T1.Range("A" & Rows.Count).End(xlUp).Row
If lr < 7 Then lr = 7

For Each MyCells In T1.Range("C7:C" & lr)
   If MyCells = "ITEM 6" Then
      T1.Range([COLOR=rgb(226, 80, 65)]"A" & MyCells.Row & ":U" & MyCells.Row[/COLOR]).Copy S1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
      T1.Range("A" & MyCells.Row & ":U" & MyCells.Row).ClearContents
      ls = S1.Range("A" & Rows.Count).End(xlUp).Row
      If ls < 7 Then ls = 7

[COLOR=rgb(44, 130, 201)]      With S1.Range("A7:A" & ls)
         Do
            e = e + 1
         Loop Until IsError(Application.Match(e, S1.Cells, 0))
     End With
     S1.Cells(ls, "A") = e[/COLOR]
  End If
Next MyCells
 
Upvote 0
So apparently I can't use color tags within VBA code tags, and I can't edit post. So here's the revised post:
VBA Code:
Dim e&, ls&, lr&, MyCells As Range
Dim T1 As Worksheet, S1 As Worksheet

Set T1 = Sheets(“Data1”)
Set S1 = Sheets(“Data2”)

e = 0
lr = T1.Range("A" & Rows.Count).End(xlUp).Row
If lr < 7 Then lr = 7

For Each MyCells In T1.Range("C7:C" & lr)
   If MyCells = "ITEM 6" Then
      T1.Range("A" & MyCells.Row & ":U" & MyCells.Row).Copy S1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
      T1.Range("A" & MyCells.Row & ":U" & MyCells.Row).ClearContents
      ls = S1.Range("A" & Rows.Count).End(xlUp).Row
      If ls < 7 Then ls = 7

      With S1.Range("A7:A" & ls)
         Do
            e = e + 1
         Loop Until IsError(Application.Match(e, S1.Cells, 0))
     End With
     S1.Cells(ls, "A") = e
  End If
Next MyCells
 
Upvote 0
Solution
Not sure what the blue segment of the code does, but your problem is probably because you are only copying the last row with lr. Change it to reference MyCells.Row and it should work fine.

VBA Code:
Dim e&, ls&, lr&, MyCells As Range
Dim T1 As Worksheet, S1 As Worksheet

Set T1 = Sheets(“Data1”)
Set S1 = Sheets(“Data2”)

e = 0
lr = T1.Range("A" & Rows.Count).End(xlUp).Row
If lr < 7 Then lr = 7

For Each MyCells In T1.Range("C7:C" & lr)
   If MyCells = "ITEM 6" Then
      T1.Range([COLOR=rgb(226, 80, 65)]"A" & MyCells.Row & ":U" & MyCells.Row[/COLOR]).Copy S1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
      T1.Range("A" & MyCells.Row & ":U" & MyCells.Row).ClearContents
      ls = S1.Range("A" & Rows.Count).End(xlUp).Row
      If ls < 7 Then ls = 7

[COLOR=rgb(44, 130, 201)]      With S1.Range("A7:A" & ls)
         Do
            e = e + 1
         Loop Until IsError(Application.Match(e, S1.Cells, 0))
     End With
     S1.Cells(ls, "A") = e[/COLOR]
  End If
Next MyCells
That is what I was asking? Why copy the same row over and over again and after copy delete another row :)
 
Upvote 0
Thanks guys. It's working now.

It's funny how I miss that! Lol.
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,194
Members
448,951
Latest member
jennlynn

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