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

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,890
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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,151
Office Version
  1. 2016
Platform
  1. Windows
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?
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,890
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,151
Office Version
  1. 2016
Platform
  1. Windows
Hard to visualize. Can you copy or upload sample data?
 

aRandomHelper

Active Member
Joined
Jan 14, 2021
Messages
278
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

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
 

aRandomHelper

Active Member
Joined
Jan 14, 2021
Messages
278
Office Version
  1. 2016
Platform
  1. Windows
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
 
Solution

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,151
Office Version
  1. 2016
Platform
  1. Windows
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 :)
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,890
Office Version
  1. 2016
Platform
  1. Windows
Thanks guys. It's working now.

It's funny how I miss that! Lol.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,511
Messages
5,636,759
Members
416,938
Latest member
sc58963

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
Top