Copy and paste to the last row in a new worksheet - vba

I'm currently trying to copy from one worksheet and paste into a new worksheet using VBA. however i have the following variables
The worksheet with data being copied is dynamic. the columns stay the same but the rows change every week.
The new worksheet which the data will be added to is dynamic. The Columns are the same but the new worksheet rows increase every week( e.g row 700 one week row 800 the next).
The columns (A and BC) that are being copied and pasted are consistent.
The data should be pasted on columns A- BC on the next available row
of the new worksheet
I've currently managed to come up with the following code but i keep getting error's and don't know where i am going wrong as i am new to VBA.
Sub CandP()
'
'
Dim Last_Row As Long
Application.ScreenUpdating = False
Last_Row = Range("A2:BC2" & Rows.Count).End(xlUp).Row
Range("A2:BC2").Copy
Windows("Newsheet.xlsm").Activate
Range("$A2:BC$" & last_row).FillDown
End Sub
All help is appreciated, Thank you

You could try this:
Option Explicit
Sub CandP()
Dim Last_Row1 As Long, Last_Row2 As Long
Dim WB1 As Workbook, WB2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set WB1 = ThisWorkbook ' Workbook where you want to copy the data
Set ws1 = WB1.Sheets("Sheet1") ' Change the name of your Sheet
Set WB2 = Workbooks.Open("C:\Desktop\vba\Newsheet.xlsm") ' Enter the address of the Workbook you want to paste the data
Set ws2 = WB2.Sheets("Sheet1") ' Change the name of your Sheet
Last_Row1 = ws1.Range("A" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
Last_Row2 = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data
ws1.Range("A2:BC" & Last_Row1).Copy ws2.Range("A" & Last_Row2)
End Sub

Related

Copy in VBA info from one data sheet already open to other data sheet from other location

I want to copy dates from a sheet name MasterData which contain macro in a file sheet from other location name Data base .At the end I want to clear info from MasterData sheet and to close sheet Data base.
I run below code but nothing is happening.
Please can advise?
I'm kind new in running VBA code...
Thank you.
Mari
Sub Copy_Paste_Below_Last_Cell()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Workbooks.Open Filename:="D:\VBA\Test1\Prices_Database_ For_ Volume.xlsx"
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("MacroMaster file.xlsm").Sheets("MasterData")
Set wsDest = Workbooks("Prices_Database_ For_ Volume.xlsx").Sheets ("DataBase")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("A2:AB100" & lCopyLastRow).Copy _
wsDest.Range("A" & lDestLastRow)
'Workbooks("Prices_Database_ For_ Volume.xlsx").Close SaveChanges:=True
'Workbooks("MacroMaster file.xlsm").Worksheets("MasterData").Range ("A2:AB100").ClearContents
End Sub
Hope is more ok now
Here is a modification of your code with out all the variables. It incorporates the last rows into each range.
Workbooks.Open Filename:="D:\VBA\Test1\Prices_Database_ For_ Volume.xlsx"
With ThisWorkbook.Sheets("MasterData")
Range("A2:AB" & Cells(Rows.Count, "A").End(xlUp).Row).Copy _
Destination:=Workbooks("Prices_Database_ For_ Volume.xlsx").Sheets("DataBase").Range("A" & Rows.Count).End(xlUp).Offset(1)
End With

How to copy rolling 3 values to another sheet using VBA

I have a pivot table as in Image. I would like to copy and paste the rolling last 3 months values to another sheet. That is i need to copy NOV,DEC,JAN values to another sheet.That is Cell value A17:C17, A18:C18 and A7:C7.
For next month it must automatically copy DEC,JAN,FEB values. I used the below but i cant get the logic. I need to copy twice. First for Cells A,B,C and for cell A,D,E. How to change the code Help me
Sub Data()
Dim sws, dws As Worksheet
Dim lr1 As Long
Dim rng1 As Range
Set sws = Sheets("AVG")
Set dws = Sheets("Data")
lr1 = sws.Cells(Rows.Count, "B").End(xlUp).Row
Set rng1 = Union(sws.Range("E" & lr1).Offset(-2, -4).Resize(3), sws.Range("E" & lr1).Offset(-2, -1).Resize(3), sws.Range("E" & lr1).Offset(-2).Resize(3))
rng1.Copy dws.Range("A28")
Sheets("AVG").Range("A" & Rows.Count).End(xlUp).Offset(-2, -2).Resize(3, 3).Copy Sheets("Data").Range("A33")
End Sub

Copy last row of a sheet and paste in last row of a different sheet VBA

Right now I have a Workbook containing a master sheet and multiple individual customer sheets. I am writing some code to look at the customer column, copy the row and then paste it in their respective sheet. At the end I want the last row from my template sheet to be pasted as the last row for the customer sheet. This is to calculate averages. So far it works but the last row gets pasted to the top of the sheet but not the bottom. I cant figure out how to get it to be the last row.
Sub copyPasteDataCustomer()
Dim sws As Worksheet
Dim tws As Worksheet
Dim cel As Range
Set sws = Sheets("Master")
For Each cel In sws.Range("B5:B" & Range("B" & Rows.Count).End(xlUp).Row)
Set tws = Sheets(CStr(cel.Value))
cel.EntireRow.Copy tws.Range("A" & Rows.Count).End(xlUp).Offset(1)
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Canvus")
Dim ws2 As Worksheet: Set ws2 = tws
For i = 2 To ws1.Range("G" & Rows.Count).End(xlUp).Row
ws1.Rows(i).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, "G").End(xlUp))
Next i
Next cel
End Sub
I ended up deleting the rest of the canvas template to just have the bottom row on the sheet. Works awesome now.

Copy specific range to first available row in another worksheet and transpose

The code below runs smoothly, and copies the data from Workbook1 to Workbook2 on the first available row, starting from column B. I need to know when the data was submitted, and therefore want the time and date to be inserted into the first available cell in column A each time data is submtited. Thanks for your help!
Option Explicit
Sub MoveData()
'Define variables
Dim Workbook1 As Workbook
Dim Workbook2 As Workbook
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long, DestLastRow As Long
'Set wb
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
'Copy (In this case I want to copy range D4:D7 only, and this will be the same every time)
ThisWorkbook.Sheets("Sheet1").Range("D4:D7").Copy
'Open Workbook 2 and paste data (transposed) on first available row starting in column B
Set Workbook2 = Workbooks.Open("H:\Macro FSC\Forsøk10\Workbook2.xlsm")
With Workbook2.Sheets("Sheet1")
' find last row with data in destination workbook "Workbook2.xlsm"
DestLastRow = .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Row
'paste special only values, and transpose
.Range("B" & DestLastRow).PasteSpecial xlValues, Transpose:=True
End With
'Save and close
Workbook2.Save
Workbook2.Close
End Sub
Try this, it's a one line solution
Option Explicit
Sub MoveData()
'Define variables
Dim Workbook1 As Workbook
Dim Workbook2 As Workbook
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long, DestLastRow As Long
'Set wb
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
'Copy (In this case I want to copy range D4:D7 only, and this will be the same every time)
ThisWorkbook.Sheets("Sheet1").Range("D4:D7").Copy
'Open Workbook 2 and paste data (transposed) on first available row starting in column B
Set Workbook2 = Workbooks.Open("H:\Macro FSC\Forsøk10\Workbook2.xlsm")
With Workbook2.Sheets("Sheet1")
' find last row with data in destination workbook "wbDatabase.xlsm"
DestLastRow = .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Row
'paste special only values, and transpose
.Range("B" & DestLastRow).PasteSpecial xlValues, Transpose:=True
'Added line here:
.range("A1").Value = now
End With
'Save and close
Workbook2.Save
Workbook2.Close
End Sub

Copy and Paste Largest value in a column from one workbook to another

I am attempting to first, find the the largest value in a column (C), then copy and paste that value into the next empty cell in 'Row 3' in a different (master) workbook. The macro I am running is found in the master workbook. I found this code that i believe will get the pasted cell into the correct spot, but I could use assistance in the code for how to find the largest cell in column C in the data workbook, and then copying and pasting that value.
Private Sub CommandButton1_Click()
Dim wsMaster As Worksheet, wbDATA As Workbook
Dim NextColumn As Long, LastRow As Long
Set wsMaster = ThisWorkbook.Sheets("Contract Metrics")
NextColumn = wsMaster.Range("C", 3).End(xlUp).Column + 1
Set wbDATA = Workbooks.Open("C:\Documents and Settings\Michael Palkovitz\My Documents\Test\Contracts Metrics.xlsx")
wbDATA.Close False
End Sub
Try this. First sort the column you need the value from, then get the last row and place the value into your first empty column in row 3 of your master sheet.
' Create an excel application and open the workbook containing the data
Dim app As Object
Dim wb As Object
Dim ws As Object
Set app = CreateObject("Excel.Application")
Set wb = app.Workbooks.Open("C:\Workbook1")
Set ws = wb.Sheets(1)
' Get last row with a value to use for the sort range
Dim last As Long
Dim value As Long
With ws
last = ws.Cells(ws.Rows.Count, 3).End(xlUp).row
.Range("C1:C" & last).Sort Key1:=.Range("C2"), order1:=xlAscending, Orientation:=xlTopToBottom
value = .Cells(last, 3)
End With
' Get the last filled cell and move over one to get the empty column
Dim col As Long
col = ActiveSheet.Cells(3, 1).End(xlToRight).Offset(0, 1).Column
ActiveSheet.Cells(3, col).value = value
wb.Close False
Set ws = Nothing
Set wb = Nothing
Set app = Nothing