Excel Copy Paste Area not the Same - excel

Hello I have this Excel Macro of generating a new sheet by copy pasting certain stuff from an existing sheet. Basically the first three paragraphs are being repeated thrice, and the final 10th paragraph is just to populate a column. However for the 7th, 8th and 9th para, its showing me
'Error 1004 copy and paster areas are not the same' whereas it is working above without any such problem.
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
Sheets("Sheet1").Move Before:=Sheets(2)
Range("A1").Select
ActiveCell.FormulaR1C1 = "Product Number"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Product Desc"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Service Type"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Service Level"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Service P/N"
Range("F1").Select
ActiveCell.FormulaR1C1 = "APAC(USD)"
Range("F2").Select
Columns("A:A").ColumnWidth = 14.27
Columns("B:B").ColumnWidth = 15.13
Columns("C:C").ColumnWidth = 13.27
Columns("D:D").ColumnWidth = 13.13
Columns("E:E").ColumnWidth = 14.33
Columns("F:F").ColumnWidth = 12.07
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste
Columns("B:B").ColumnWidth = 40
Sheets("1. SMARTnet ").Select
Range("D9:E20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E2").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("D1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D2").Select
ActiveSheet.Paste
Columns("D:D").ColumnWidth = 17.07
Range("D3").Select
ActiveSheet.Paste
Range("D2:D3").Select
Selection.AutoFill Destination:=Range("D2:D20687"), Type:=xlFillDefault
Range("D2:D20687").Select
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A20688").Select
ActiveSheet.Paste
Sheets("1. SMARTnet ").Select
Range("F9:G20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E20688").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("F1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D20688").Select
ActiveSheet.Paste
Range("D20689").Select
ActiveSheet.Paste
Range("D20688:D20689").Select
Selection.AutoFill Destination:=Range("D20688:D41373"), Type:=xlFillDefault
Range("D20688:D41373").Select
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A41374").Select
ActiveSheet.Paste
Sheets("1. SMARTnet ").Select
Range("H9:I20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E41374").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("H1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D41374").Select
ActiveSheet.Paste
Range("D41375").Select
ActiveSheet.Paste
Range("D41374:D41375").Select
Selection.AutoFill Destination:=Range("D41374:D62059"), Type:=xlFillDefault
Range("D41374:D62059").Select
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A62060").Select
ActiveSheet.Paste
Sheets("1. SMARTnet ").Select
Range("P9:P20694,U9:U20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E62060").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("P1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D62060").Select
ActiveSheet.Paste
Range("D62061").Select
ActiveSheet.Paste
Range("D62060:D62061").Select
Selection.AutoFill Destination:=Range("D62060:D82745"), Type:=xlFillDefault
Range("D62060:D82745").Select
Range("C2").Select
ActiveCell.FormulaR1C1 = "SMARTnet"
Selection.AutoFill Destination:=Range("C2:C82745"), Type:=xlFillDefault
Range("C2:C82745").Select
End Sub
Any hint will be appreciated :)

I think you should simplifi your code by not using "select"
for Example
instead of
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste
You can write as
Sheets("1. SMARTnet ").Range("B9:C20694").Copy Sheets("Sheet1").Range("a2")
Then you will be able to find the error better
This will also run faster because it saves screen updating time.

Related

Excel VBA - vlookup for a variable number of rows within a macro

I've created a macro to organize a data set and compile into another sheet in a way that makes more sense for doing analyses. The set originally is comprised of columns for user, timestamp and 3 possible events. The user could appear on multiple rows but I wanted to look at this data set by user and have a separate column for each timestamp. The macros I've made can successfully clean, filter by event type, and separate by event type into separate worksheets (no matter how many rows of data) but I'm having trouble with compiling data into one sheet using vlookup AND accounting for a variable number of rows. I have looked at other answers to this question and tried this:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R" & LastRow0 & "C3,2,FALSE)"
... but it keeps giving me errors.
What I have below (Vlookup_events2) works but just not for the entire variable number of rows.Please help me adjust the code for the vlookup so it will work no matter how many rows.
Here is the code below for separating data (just for reference), then the problem macro - compiling it with vlookup. I would really appreciate some help, I know there's an amazing VBA expert out there!
Sheets.Add
Sheets("Sheet1").Name = "Email Sent"
ActiveSheet.Next.Select
Selection.AutoFilter
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Email Sent"
ActiveCell.Offset(0, -2).Range("A1:D2355").Select
ActiveCell.Activate
Selection.Copy
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Next.Select
Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Email Opened"
Sheets.Add
Sheets("Sheet2").Name = "Email Opened"
ActiveSheet.Next.Select
ActiveCell.Range("A1:D1000000").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 3).Range("A1").Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveCell.Columns("A:A").EntireColumn.Select
Selection.Delete Shift:=xlToLeft
Sheets.Add
Sheets("Sheet3").Name = "Clicked Link"
ActiveSheet.Next.Select
ActiveSheet.Next.Select
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Clicked Link"
ActiveCell.Offset(0, -2).Range("A1:D1000000").Select
ActiveCell.Activate
Selection.Copy
ActiveSheet.Previous.Select
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 3).Columns("A:A").EntireColumn.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
Sub Vlookup_events2()
' Vlookup_events2 Macro
ActiveSheet.Previous.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
ActiveSheet.Next.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
ActiveSheet.Next.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
Sheets.Add
Sheets("Sheet4").Name = "Compiled Events"
ActiveSheet.Previous.Select
ActiveSheet.Previous.Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveSheet.Next.Select
ActiveSheet.Next.Select
Range("A1").Select
ActiveSheet.Paste
Columns("C:D").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("B1").Select
ActiveCell.FormulaR1C1 = "Email Sent Time"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Email Opened Time"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Clicked Link Time"
Range("A1").Select
Application.Goto Reference:="R2C3"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
Range("C3").Select
Range(Selection, Selection.End(xlUp)).Select
Columns("C:C").Select
Selection.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
Columns("D:D").Select
Selection.FormulaR1C1 = "=VLOOKUP(RC[-3],'Clicked Link'!R1C1:R56C3,2,FALSE)"
Columns("C:C").Select
Selection.NumberFormat = "m/d/yyyy h:mm"
Columns("D:D").Select
Selection.NumberFormat = "m/d/yyyy h:mm"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Email Opened Time"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Clicked Link Time"
Range("C2").Select
End Sub

Run-time error 1004 while copy-pasting data

I have written a macro to list down all the numbers between certain ranges and then to paste values against each number. The macro throws a Run-time error 1004. Here's the macro:
Sub Populate()
Range("A1") = Range("H18").Value
Range("A1").Select
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=Range("I18").Value, Trend:=False
Range("J18").Select
Selection.Copy
Range("B1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
Selection.End(xlDown).Select
Range("B20").Select
Range(Selection, Selection.End(xlUp).Offset(-1, 0)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("B1").Select
Range("A1").End(xlDown).Offset(1, 0) = Range("H19").Value
Range("A1").End(xlDown).Select
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=Range("I19").Value, Trend:=False
Range("J18").Select
Selection.Copy
Range("B1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
Selection.End(xlDown).Select
Range("B20").Select
Range(Selection, Selection.End(xlUp).Offset(-1, 0)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("B1").Select
End Sub
Try changing the beginning of your syntax to:
Dim dRange As Range
Set dRange = Range("A1:I18")
Range("A1") = Range("H18").Value
Range("A1").Select
dRange.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay
You will probably still run into issues due to the following code being repeated:
Range("B20").Select
Range(Selection, Selection.End(xlUp).Offset(-1, 0)).Select
Not sure exactly what the end goal is with this, but from my testing this gets you closer and takes care of your Run-time error: '1004'

Error copying data to new Excel sheet using a Macro

This is my first post here.
I am trying to copy data from one sheet to a new sheet in the same workbook after filtering in the main sheet.
I also have to copy the sheet template from a template sheet onto this new sheet where I am copying the data to before I copy the data.
This is the VBA code shown in my macro:
Sub Macro7()
'
' Macro7 Macro
'
'
Sheets("Template").Select
Rows("1:3").Select
Application.CutCopyMode = False
Selection.Copy
Dim sSheetName As String
Sheets.Add After:=Sheets(Sheets.Count)
sSheetName = ActiveSheet.Name
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("N13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("D4").Select
ActiveSheet.Paste
Columns("D:D").EntireColumn.AutoFit
Sheets("Sheet1").Select
Range("A13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("C4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("D13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("E4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("F4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("H13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("G4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("F13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("I4").Select
ActiveSheet.Paste
Range("A4").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Fives Cinetic Corp"
Columns("B:B").Select
Columns("A:A").ColumnWidth = 17.57
Range("A4").Select
Selection.AutoFill Destination:=Range("A4:A5")
Range("A4:A5").Select
Range("D10").Select
End Sub
I get an error: Run-time Error 9: Subscript out of range
Guess its something to do with sheet numbers but not able to figure it out exactly what it is.
Just incase you haven't solved this already, take a look at the code I've adapted for you. When making new VBA projects, play around with some of these methods, you'll find they're a lot faster and more reliable than your previous version. Compare them side by side.
This took a lot of assumption on my part so make a backup before running this to test it and make sure everything is going to the right place.
Sub Macro7()
Sheets.Add After:=Sheets(Sheets.Count)
NewSheet = ActiveSheet.Name
Sheets("Template").Rows("1:3").Copy Destination:=ActiveSheet.Range("A1")
Sheets("Sheet1").Activate
Range("N13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("D4")
With Sheets(NewSheet)
Columns("D:D").EntireColumn.AutoFit
End With
Range("A13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("C4")
Range("D13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("E4")
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("F4")
Range("H13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("G4")
Range("F13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("I4")
Sheets(NewSheet).Activate
Range("A4").Value = "Fives Cinetic Corp"
Columns("A:A").AutoFit
Range("A4").AutoFill Destination:=Range("A4:A5")
Range("D10").Select
End Sub

calculating network days, with some dates omitted from exceptions - Excel

The following is a macro which collects data from data we import into certain tabs. When the macro is run it filters through the data and produces a new excel book with this new data. The person who created this is no longer with us. The macro works fine except I'm trying to add another column like the one that calculates network days called days since 1st auth less parked. Im wanting to add another which gives days since the start of the information being passed to us. Ie a column call 1st instructed less parked.
Sub Runme()
'
' Macro1 Macro
' Macro recorded 22/03/2013'
'
Sheets("CCX data SORTED").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("CCX Data Raw").Select
Range("A:C,E:G").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.AutoFilter
Range("H2").Select
Range("A1:X10000").Sort Key1:=Range("H1"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("H55000").End(xlUp)(2, 1).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.ClearContents
Cells.Select
Selection.Copy
Sheets("CCX data SORTED").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("CCX Data Raw").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("CCX data SORTED").Select
Columns("X:X").Select
Selection.Insert Shift:=xlToRight
Range("X2").Select
ActiveCell.FormulaR1C1 = _
"=IF(VLOOKUP(RC[-23],'SCMT weekly data'!C[-17]:C[-11],7,FALSE)>0,(VLOOKUP(RC[-23],'SCMT weekly data'!C[-17]:C[-11],7,FALSE)),"""")"
Range("X2").Select
Selection.AutoFill Destination:=Range("X2:X5000")
Range("X1").Select
ActiveCell.FormulaR1C1 = "SCMT end Date"
Columns("X:X").Select
Selection.NumberFormat = "m/d/yyyy"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SCMT Weekly data").Select
Columns("Z:Z").Select
Selection.Insert Shift:=xlToRight
Range("Z1").Select
ActiveCell.FormulaR1C1 = "Days To Exclude 1"
Range("Z2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-2],RC[-1])=2,NETWORKDAYS(RC[-2],RC[-1],'Bank hols'!RC[-25]:R[56]C[-25]),0)"
Selection.AutoFill Destination:=Range("Z2:Z5000"), Type:=xlFillDefault
Columns("AD:AD").Select
Selection.Insert Shift:=xlToRight
Range("AD1").Select
ActiveCell.FormulaR1C1 = "Days To Exclude 2"
Range("AD2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-2],RC[-1])=2,NETWORKDAYS(RC[-2],RC[-1],'Bank hols'!RC[-29]:R[56]C[-29]),0)"
Selection.AutoFill Destination:=Range("AD2:AD5000")
Columns("Z:Z").Select
Selection.NumberFormat = "0"
Columns("AD:AD").Select
Selection.NumberFormat = "0"
Range("AH2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("AF1").Select
ActiveCell.FormulaR1C1 = "Days since first auth"
Range("AF2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-21],R2C34)=2,NETWORKDAYS(RC[-21],R2C34,'Bank hols'!RC[-31]:R[56]C[-31]),"""")"
Range("AG2").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISNUMBER(RC[-1]),SUM(RC[-1]-(RC[-3]+RC[-7])),"""")"
Range("AG1").Select
ActiveCell.FormulaR1C1 = "Days since 1st Auth less parked"
Columns("AH:AH").Select
Selection.Insert Shift:=xlToRight
Range("AH1").Select
ActiveCell.FormulaR1C1 = "Still Parked"
Range("AH2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(ISNUMBER(RC[-10]),ISBLANK(RC[-9])),""XX"",IF(AND(ISNUMBER(RC[-6]),ISBLANK(RC[-5])),""XX"",""""))"
Selection.AutoFill Destination:=Range("AH2:AH5000")
Range("AF2:AG2").Select
Selection.AutoFill Destination:=Range("AF2:AG5000")
Range("A55000").End(xlUp)(2, 1).Select
Selection.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("CCX data SORTED").Select
Range("Z1").Select
ActiveCell.FormulaR1C1 = "SCMT Queue"
Range("Z2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-25],'SCMT Daily Drop'!C[-16]:C[-14],3,FALSE)"
Selection.AutoFill Destination:=Range("Z2:Z5000")
Range("AA1").Select
ActiveCell.FormulaR1C1 = "Days Since First Approved"
Range("AA2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,'SCMT weekly data'!C7:C34,27,FALSE)"
Selection.AutoFill Destination:=Range("AA2:AA5000")
Range("AB1").Select
ActiveCell.FormulaR1C1 = "Still Parked"
Range("AB2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,'SCMT weekly data'!C7:C34,28,FALSE)"
Selection.AutoFill Destination:=Range("AB2:AB5000")
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A55000").End(xlUp)(2, 1).Select
Selection.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("SCMT weekly data").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(Array("SCMT weekly data", "SCMT Daily Drop", "CCX data SORTED")).Select
Sheets("CCX data SORTED").Activate
Sheets(Array("SCMT weekly data", "SCMT Daily Drop", "CCX data SORTED")).Copy
Windows("SCMT Parked.xls").Activate
Sheets("SCMT weekly data").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("SCMT Daily Drop").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("CCX data SORTED").Select
Cells.Select
Selection.Delete Shift:=xlUp
MsgBox ("Macro Complete")
End Sub
Where do you want it? IF you want it in the z column, for example, try:
Columns("Z:Z").Select
Selection.Insert Shift:=xlToRight
Range("Z1").Select
ActiveCell.FormulaR1C1 = "New formula"
Range("Z2").Select
ActiveCell.FormulaR1C1 = "=AA2-AB2"
Selection.AutoFill Destination:=Range("Z2:Z5000")
I don't understand the formula you need, but there's a template you can use... If you give us the formula we can put it the second last line?

How to create a macro to copy data from one excel worksheet to the next line in another worksheet

I'm a bit stumped with this and wondered if anyone could help please? I've created a macro in excel to copy some data from one worksheet to another. It works fine, but obviously each time I run the macro, it just completes the top line (as I pasted to whilst recording). I actually want it work so that every time I run the macro, it copies the data to a new line.
If it helps, I can paste a link to the document. I'm sure it's just a case of pasting some VBA code into the macro. At the moment, the code looks like this:
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets("Quotation System").Select
Range("K9").Select
Selection.Copy
Sheets("Confirmed Bookings").Select
Range("A2").Select
ActiveSheet.Paste
Range("B2").Select
Sheets("Quotation System").Select
Range("K11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
ActiveSheet.Paste
Sheets("Quotation System").Select
Range("K13").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
Range("C2").Select
ActiveSheet.Paste
Sheets("Quotation System").Select
Range("K15").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
Range("D2").Select
ActiveSheet.Paste
Columns("D:D").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Columns("D:D").ColumnWidth = 10.86
Sheets("Quotation System").Select
Range("K17").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
Range("E2").Select
ActiveSheet.Paste
Range("F2").Select
Sheets("Quotation System").Select
Range("K19").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
ActiveSheet.Paste
Columns("F:F").ColumnWidth = 8.57
Sheets("Quotation System").Select
Range("K21").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
Range("G2").Select
ActiveSheet.Paste
Range("H2").Select
Sheets("Quotation System").Select
Range("K23").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
ActiveSheet.Paste
Range("I2").Select
Sheets("Quotation System").Select
Range("K25").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
ActiveSheet.Paste
Sheets("Quotation System").Select
Range("K7").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
Range("J2").Select
ActiveSheet.Paste
Range("K2").Select
Sheets("Quotation System").Select
Range("G29").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Confirmed Bookings").Select
ActiveSheet.Paste
Columns("K:K").EntireColumn.AutoFit
Columns("K:K").ColumnWidth = 6
Columns("K:K").ColumnWidth = 7
Range("K2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "='Quotation System'!R[27]C[-4]"
Range("K3").Select
Columns("J:J").EntireColumn.AutoFit
Range("I19").Select
Columns("A:A").ColumnWidth = 8.43
End Sub
Sub Booking_confimred()
End Sub
Many thanks!
Because you have a very situation-specific macro recorded, I would like to show you just a basic method to improve this macro and place those copies to a specific position:
You start with this
Public Sub Macro1()
Application.CutCopyMode = False
then, this is what macro-recoder uses to copy:
Sheets("Quotation System").Select
Range("K9").Select
Selection.Copy
This is what you can use instead:
Sheets("Quotation System").Range("K9").Copy
This is what macro-recorder uses to paste:
Sheets("Confirmed Bookings").Select
Range("A2").Select
ActiveSheet.Paste
This is what you can use instead:
Sheets("Confirmed Bookings").Range("A2").Paste
And in order to paste it to a new line, it would be like
With Sheets("Confirmed Bookings")
.Cells(.UsedRange.Columns(1).Rows.Count + 1, 1).Paste
End With
However, this is not the only way to solve this, and sureley not the best one, but it might help you at your level of understanding VBA.
If you want a neater solution, you might want to search around on SO, as there are many similar questions/problems and good solutions to them here.

Resources