How to copy entire data using row by row and paste to another sheet - excel

I am trying to copy my data from one worksheet to another worksheet. I want to copy the first row and paste then copy the second row and then paste on the next empty row in the target sheet. Actually I want to copy data using row by row and in loop until the end of the row is reached in the data sheet. when the macro reach at the end of the row, and there is no data on the last row then it show pop up finish message.
I am trying following code but it is not fulfil my needs. Any suggest and help will be highly appreciated. Thanks
Sub InsertData()
Dim wsCopy As Worksheet, wsDest As Worksheet
Dim lCopyLastRow As Long, lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Warranty Template.xlsm").Worksheets("PivotTable")
Set wsDest = Workbooks("QA Matrix Template.xlsm").Worksheets("Plant Sheet")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, 1).End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, 4).End(xlUp).Offset(1,0).Row
'3. Copy & Paste Data
wsCopy.Range("A5:A" & lCopyLastRow).Copy _
wsDest.Range("D" & lDestLastRow)
End Sub

I guess there is two ways of solving your task (see part 3)
Sub InsertData()
Dim wsCopy As Worksheet, wsDest As Worksheet
Dim lCopyLastRow As Long, lDestLastRow As Long, row As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Warranty Template.xlsm").Worksheets("PivotTable")
Set wsDest = Workbooks("QA Matrix Template.xlsm").Worksheets("Plant Sheet")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, 1).End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count,4).End(xlUp).Offset(1,0).Row
'3. Copy & Paste Data
For row = 5 To lCopyLastRow ' Cycle form 5th row (as well as you used range from A5) to last filled row (in A column)
' First: by copying data (not effective way, your solution is better)
wsCopy.Range("A" & row).Copy wsDest.Range("D" & (lDestLastRow + (row - 5)))
' Second: by filling the data
' Like this: wsDest.Range("D" & (lDestLastRow + (row - 5))) = wsCopy.Range("A" & row)
Next row
End Sub

Related

Update data from copy-paste in excel is located on bellow the old data. How can I change the location?

I got some problem on excel macro and the code like this :
Sub Backup_button1()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'set variable for copy and destination sheets
Set wsCopy = Workbooks("Form Input SAP.xlsm").Worksheets("7-9")
Set wsDest = Workbooks("File Backup.xlsx").Worksheets("7-9")
'1. Find last used row in the copy range based on data in column
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "H").End(xlUp).Row
'2. Find first blank row in the destination range based on data in colom Offset property move down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "H").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("H4:N" & lCopyLastRow).Copy
wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
End Sub
Those code is used to copy some data in excel workbooks and paste to different workbooks. If there some old data in destination file, it will make an update bellow the old data. The code is work properly, but I want to change update location not in bellow old data but it will located in cells after the old data. Any advice?
Change your code:
Const DestRow = 4
'2. Find first blank column in the destination range
lDestLastCol = wsDest.Cells(DestRow, wsDest.Columns.Count).End(xlToLeft).Column
'3. Copy & Paste Data
wsCopy.Range("H4:N" & lCopyLastRow).Copy
wsDest.cells(DestRow, lDestLastCol+1).PasteSpecial Paste:=xlPasteValues
Step 2 will give you the last column in use, Step 3 will copy the data into the next free column. I have assumed that in the destination sheet your data also starts at row 4, else you have to change the const definition.

Excel VBA copy single column from table and transpose

I'm trying to copy a column from a table without it's header and transposing it into another part of the workbook.
To do so I've taken a piece of code that I've used before but can't quite tweak it to do what I want.
I was wondering if you could please help me?
I have table in "sheet 1" that has two columns and starts in cell "A3". I'm trying to copy column B, without the header, and transpose it into "sheet 2" from the cell "J2".
I can't do it via the macro recorder because if the table in sheet 1 only has one row it won't transpose into sheet 2 because it copies too many cells (and I'm learning more on how to avoid macro recorder).
This is the code I've tweaked, any help on how I can change it or use a better code?
'
' Macro21 Macro
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Worksheets("Sheet1")
Set wsDest = Worksheets("Sheet2")
'1. Find last used row in the copy range based on data in column 1
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
'2 Find first bnak row in the destination range based in column B
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("B4").Copy wsDest.Range("J2" & lDestLastRow)
End Sub
Thanks
To copy a range and then paste it transposed, you can of course use .Copy and .PasteSpecial Transpose:=True, but it will be much better to resize your destination range in such a way that you shift the orientation of your copy range, and then to apply Application.Transpose() to the rngCopy.Value.
This code should do it. Some elaboration on your comments in there to explain what everything does.
Sub TransposeRangeColumn()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim rngCopy As Range
'Set variables for copy and destination sheets
Set wsCopy = Worksheets("Sheet1")
Set wsDest = Worksheets("Sheet2")
'1. Find last used row in the copy range based on data in column B (!? you had "column 1")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
'2. Set rngCopy
Set rngCopy = wsCopy.Range("B4:B" & lCopyLastRow)
'3. a) Resize destRang transposed. Example:
'Range("A1").Resize(RowSize=2, ColumnSize=3) would get you Range("A1:C2")
'we need to transpose, so input .Resize(rngCopy.ColumnSize, rngCopy.RowSize) instead
'we have 1 column, so just use 1 for the row; for columns, count rows rngCopy
'b) now that we have a transposed destination range, we want to set its value equal to
'a transposed version of rngCopy using Application.Transpose()
wsDest.Range("J2").Resize(1, rngCopy.Rows.Count).Value = Application.Transpose(rngCopy.Value)
'Code below would also have worked, but try to grow accustomed to using .Value = .Value instead
'it gives way better performance
'rngCopy.Copy
'wsDest.Range("J2").PasteSpecial Transpose:=True, Paste:=xlPasteValues
End Sub
You mentioned that your range is a table. If it is an actual Excel Table, you don't have to worry about finding/defining the first and last row of rngCopy. You can just set your range to the .DataBodyRange of the specific column you want (here: Column 2). Like this:
Sub TransposeTableColumn()
'Transpose if it's an actual table
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim rngCopy As Range
Set wsCopy = Worksheets("Sheet1")
Set wsDest = Worksheets("Sheet2")
'Use your table name instead of "Table1"
Set rngCopy = wsCopy.ListObjects("Table1").ListColumns(2).DataBodyRange
wsDest.Range("J2").Resize(1, rngCopy.Rows.Count) = Application.Transpose(rngCopy.Value)
End Sub
No need to use the clipboard and copy/paste operations. Do I direct write to cells and use WorksheetFunction.Transpose() to make the column into a row
Here is the code that worked for me
Option Explicit
Public Sub TestCopy()
CopyColumnTransposedTo
Sheets("Sheet1").Range ("A3"), _
2, _
Sheets("Sheet2").Range("J2")
End Sub
Public Sub CopyColumnTransposedTo(ByVal r_table As Range, column As Long, ByVal r_destination As Range)
' Move to the column on table
Set r_table = r_table.Cells(1, column)
' Count rows from end
Dim ws As Worksheet
Set ws = r_table.Worksheet
Dim count As Long
count = ws.Cells(ws.Rows.count, r_table.column).End(xlUp).Row - r_table.Row + 1
If count > 0 Then
' Copy transpose to destination
r_destination.Resize(1, count) = _
WorksheetFunction.Transpose( _
r_table.Resize(count, 1).Value)
End If
End Sub
Example results

Copy and Paste data from one workbook to another from specified cell

I have to prepare a macro for tickets. I have workbook (Test100). User open this workbook. If in column F in this workbook, user gonna change value (doen's matter what is the change, it can be just typping "x" inside the cell), macro should:
a) open new workbook (Test02) in specified location (always the same),
b) copy values from 2 cells in specified columns (A & B) from Test100 into FIRST BLANK row in specified columns (also A & B) in opened workbook - Test02
Test100 - from this I want to copy data
Test02 - here I want to paste the data
c) The point is to copy values from columns A & B but also from the ROW number in where somebody change the value in column F. Sometimes the mentioned values will be in row 3, another time in row 300 - but still in the same columns.
I wrote the code which works for points a) and b), but I have no idea what should I do with point c :( Could you help me?
Below you can find the code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Open new workbook (copy destination) after summoning it by change data in column F
If Not Intersect(Target, Range("F:F")) Is Nothing Then
Workbooks.Open "HERE IS THE DESTINATION OF DESTINATED WORKBOOK"
Dim wsCopy As Worksheet, wsDest As Worksheet
Dim ACopyLastRow As Long, ADestLastRow As Long, BCopyLastRow As Long, BDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Test100").Worksheets("Arkusz1")
Set wsDest = Workbooks("Test02").Worksheets("Arkusz1")
'1. Find last used row in the copy range based on data in column A
ACopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, 1).End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
ADestLastRow = wsDest.Cells(wsDest.Rows.Count, 4).End(xlUp).Offset(1, 0).Row
'3. Copy & Paste Data
wsCopy.Range("A" & ACopyLastRow).Copy _
wsDest.Range("A" & ADestLastRow)
'4. Find last used row in the copy range based on data in column B
BCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, 1).End(xlUp).Row
'5. Find first blank row in the destination range based on data in column B
'Offset property moves down 1 row
BDestLastRow = wsDest.Cells(wsDest.Rows.Count, 4).End(xlUp).Offset(1, 0).Row
'6. Copy & Paste Data
wsCopy.Range("B" & BCopyLastRow).Copy _
wsDest.Range("B" & BDestLastRow)
End If
End Sub

copy range from one workbook to another workbook and Paste Data Below the Last Used Row

workbooks
Requests to copy only entries listed in the table
Currently the range is B4: O5 (light blue color)
If there is additional data (for example B4 "O10
Who knew how to copy the above data another time
Each time he will copy only the cells that contain data.
Paste the data here as values (not formulas)
Each time he adds the data at the end of the empty table.
I found a code that works
Just does not past it as values
How can i fix it?
Is there another way to do it?
Thanks
Option Explicit
Sub Copy_Paste_Below_Last_Cell()
'Find the last used row in both sheets and copy and paste data below existing data.
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("הזמנת עבודה.xlsm").Worksheets("העתקה")
Set wsDest = Workbooks("יומן יומי.xlsm").Worksheets("יומן ראשי")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).row
'3. Copy & Paste Data
wsCopy.Range("B4:O" & lCopyLastRow).Copy _
wsDest.Range("A" & lDestLastRow)
'Optional - Select the destination sheet
wsDest.Activate
End Sub

VBA Excel Copy and paste values to new work with set range after last row

copying and pasting range of cells but only values to new workbook after last line. My copy worksheet data range starts at o3 and goes to r100. data will change. the new work book where i want to start pasting just the values starts at cell m13. by clicking button i want pull new data under a blank line.
I have researched code that either does copy and paste to different workbooks or copying to last row. but not both.
Sub xtrnsf_to_other_wrkbook()
'Find the last used row in both sheets and copy and paste data below existing data.
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("LP sort2.xlsm").Worksheets("Chalks")
Set wsDest = Workbooks("PC.xlsm").Worksheets("Planner's Checklist")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "O").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "M").End(xlUp).Offset(2).Row
'3. Copy & Paste Data
wsCopy.Range("O3:R).Copy _
wsDest.Range("M" & lDestLastRow)
End Sub
My copy worksheet data range starts at O3 and goes to r100. data will change. the new work book where i want to start pasting just the values starts at cell M13. by clicking button i want pull new data under a blank line.
You just need to change the copy line as you left out the end of the source range.
Paste special will copy the values not the formulae.
wsCopy.Range("O3:R" & lCopyLastRow).Copy
wsDest.Range("M" & lDestLastRow).pastespecial xlvalues

Resources