Can't activate worksheet VBA - vba

I am new to coding and i cant seem to solve this problem. I am trying to copy and paste some ranges from one worksheet to another. When doing so, I continue to be prompted with an error message when the code attempts to activate the new worksheet. The Code is below. The error occurs when trying to active the "Summary" sheet before copying and pasting the ranges.
Sub nxt()
LR = Cells(Rows.Count, "A").End(xlUp).Row
Last = Cells(Rows.Count, "D").End(xlUp).Row
clryellow = RGB(256, 256, 0)
ThisWorkbook.Sheets("Rankings").Select
Sheets("Rankings").Select
ActiveSheet.Range("A1:H1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Rankings").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Rankings").AutoFilter.Sort.SortFields.Add Key:= _
Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Rankings").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ThisWorkbook.Sheets("Summary").Activate
Sheets("Summary").Select
Sheets("Summary").Range("A8:A18").Value = Sheets("Rankings").Range("A2:A12").Value
Sheets("Summary").Range("B8:B18").Value = Sheets("Rankings").Range("E2:E12").Value
Sheets("Summary").Range("C8:C18").Value = Sheets("Rankings").Range("G2:G12").Value
Sheets("Summary").Range("D8:D18").Value = Sheets("Rankings").Range("H2:H12").Value
ActiveWorkbook.Sheets("Summary").Activate
With ActiveSheet
For x = Last To 8 Step -1
If (Cells(x, "D").Value) >= 6 Then
Cells(x, "A").EntireRow.Delete
ElseIf (Cells(x, 4).Value) < 6 Then
Cells(x, 1).Interior.Color = clryellow
Cells(x, 1).Font.Bold = True
Cells(x, 4).Interior.Color = clryellow
Cells(x, 4).Font.Bold = True
End If
Next x
End With
For Each Worksheet In ActiveWorkbook.Worksheets
ActiveSheet.Calculate
Next Worksheet
end sub

You can .Select one or more objects (worksheets, cells, etc) into a collection but you can only .Activate one of them. Whatever is activated is always part of the selection, even if they are both the same single object. You do not need to both .Select and .Activate an object unless you are selecting more than one and require that one of them the the ActiveCell or ActiveSheet.
Essentially, a .Select method or .Activate method should be used to bring the worksheet or range object to the user's attention. It is not necessary to select or activate something in order to work with it (your value transfer speaks to that).
Here is a short rewrite of your routine that steers away from relying on .Select and .Activate to reference objects.
Sub summarizeRankings()
Dim lstA As Long, lstD As Long, clrYellow As Long, x As Long, ws As Worksheet
With ThisWorkbook
With .Worksheets("Rankings")
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
With .Resize(.Rows.Count, 8)
.Cells.Sort Key1:=.Columns(8), Order1:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
.AutoFilter
End With
End With
Set ws = .Cells(1, 1).Parent
End With
With .Worksheets("Summary")
.Range("A8:A18").Value = ws.Range("A2:A12").Value
.Range("B8:B18").Value = ws.Range("E2:E12").Value
.Range("C8:C18").Value = ws.Range("G2:G12").Value
.Range("D8:D18").Value = ws.Range("H2:H12").Value
lstA = .Cells(Rows.Count, "A").End(xlUp).Row
lstD = .Cells(Rows.Count, "D").End(xlUp).Row
clrYellow = RGB(256, 256, 0)
For x = lstD To 8 Step -1
If (.Cells(x, "D").Value) >= 6 Then
.Cells(x, "A").EntireRow.Delete
ElseIf (.Cells(x, 4).Value) < 6 Then
.Cells(x, 1).Interior.Color = clrYellow
.Cells(x, 1).Font.Bold = True
.Cells(x, 4).Interior.Color = clrYellow
.Cells(x, 4).Font.Bold = True
End If
Next x
.Activate '<-last step brings the Summary worksheet to the front
End With
End With
Application.Calculate
End Sub
See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Related

Object defined error while looping through worksheets

I have a project I am working on that entails looping through a series of worksheets, each of which is named after a series of values in a separate sheet. I then perform some functions on each sheet, adding a formula to the next empty column. However, my code is erroring out at this line:
Worksheets(Name).Range(.Cells(2, LastColumn + 1)).Formula = "=F2"
The specific error is
"Application-defined or Object-defined error"
and I'm not sure why this is occurring. I've switched up the way I reference the worksheets, moving around the With-blocks etc. Note that this is just a Sub where I've been testing out different components of the full macro. Any help on this error or what I'm doing wrong would be appreciated!
Sub Test()
Dim ws2 As Worksheet
Dim wb As Workbook
Dim LastRow As Long, LastColumn As Long
Dim LastRow2 As Long
Dim Name As Variant, SheetR As Variant
Set wb = ActiveWorkbook
Set ws2 = wb.Sheets("Comm")
LastRow2 = 6
'sort each sheet on date descending
With wb
SheetR = ws2.Range("A3:A" & (LastRow2 + 2))
For Each Name In SheetR
LastColumn = 0
LastRow = 0
With Worksheets(Name)
Worksheets(Name).Rows("1:1").AutoFilter
Worksheets(Name).AutoFilter.Sort.SortFields.Add Key:=Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets(Name).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
LastColumn = Worksheets(Name).Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = Worksheets(Name).Cells(Rows.Count, 1).End(xlUp).Row
If LastRow = 1 Then
ElseIf LastRow = 2 Then
ElseIf LastRow = 3 Then
ElseIf LastRow = 4 Then
ElseIf LastRow > 4 Then
'The error is occurring at this next line
Worksheets(Name).Range(.Cells(2, LastColumn + 1)).Formula = "=F2"
Worksheets(Name).Range(.Cells(3, LastColumn + 1)).Formula = "=F3+O2"
Worksheets(Name).Range(.Cells(3, LastColumn + 1)).Select
Selection.AutoFill Destination:=Sheets(CStr(Name)).Range(.Cells(4, LastColumn + 1), .Cells(LastRow, LastColumn + 1)), Type:=xlFillDefault
Else
End If
End With
Next Name
End With
End Sub
Look at my annotation.
Sub Test()
Dim ws2 As Worksheet, wb As Workbook, LastRow As Long, LastColumn As Long, LastRow2 As Long, Name As Variant, SheetR As Variant
Set wb = ActiveWorkbook
Set ws2 = wb.Sheets("Comm")
LastRow2 = 6
'sort each sheet on date descending
SheetR = ws2.Range("A3:A" & (LastRow2 + 2))
For Each Name In SheetR
LastColumn = 0
LastRow = 0
With Worksheets(Name)
.Rows("1:1").AutoFilter
.AutoFilter.Sort.SortFields.Add Key:=.Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Added "." before the Key range
With .AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column 'Added "." before Columns.Count
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Added "." before Rows.Count
If LastRow = 1 Then
ElseIf LastRow = 2 Then
ElseIf LastRow = 3 Then
ElseIf LastRow = 4 Then
ElseIf LastRow > 4 Then
'The error is occurring at this next line
.Cells(2, LastColumn + 1).Formula = "=F2" 'Removed .range() as this is only a single cell being called
.Cells(3, LastColumn + 1)).Formula = "=F3+O2" 'Removed .range() as this is only a single cell being called
.Cells(3, LastColumn + 1)).Select 'Removed .range() as this is only a single cell being called
Selection.AutoFill Destination:=Sheets(CStr(Name)).Range(.Cells(4, LastColumn + 1), .Cells(LastRow, LastColumn + 1)), Type:=xlFillDefault 'Need to check your qualifiers in this line... using source, not destination
Else
End If
End With
Next Name
End Sub
Edit1: Fixed innapropriate call for range() on a single cell. Props to u/PeterT for calling it out
You've taken the time to build a With Worksheets(Name) ... End With block but failed to take advantage of it. Additionally, .Range(.Cells(...)) is bad syntax unless you provide two .Cells for a start and stop.
To rewrite your With Worksheets(Name) ... End With block,
...
With Worksheets(Name)
.Rows("1:1").AutoFilter
.AutoFilter.Sort.SortFields.Add Key:=.Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
If LastRow = 1 Then
ElseIf LastRow = 2 Then
ElseIf LastRow = 3 Then
ElseIf LastRow = 4 Then
ElseIf LastRow > 4 Then
'The error is occurring at this next line
.Cells(2, LastColumn + 1).Formula = "=F2"
.Cells(3, LastColumn + 1).Formula = "=F3+O2"
.Cells(3, LastColumn + 1).AutoFill Destination:=.Range(.Cells(4, LastColumn + 1), .Cells(LastRow, LastColumn + 1)), Type:=xlFillDefault
Else
End If
End With
...

Copy pasting the cells when they are equal to another spreadsheet using Macro

I have a work question and i want my macro to do the following
i have two columns (column A and B). Column A has the names and column B contains their info.
I want my macro to find duplicate names and copy both col A and B and paste them into another spreadsheet in the following location
C:\Users\kentan\Desktop\Managed Fund
Each spreadsheet created must contain the name of that name as the file name
I have create the macro to do the following but it's not giving me the right result
Sub IRIS()
Dim i As Integer
With ActiveSheet.Sort
.SetRange Range("A:B")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlStroke
.Apply
End With
i=1
Do Until Len(Cells(i, 1).Value) = 0
If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
Range(Cells(i, 1), Cells(i, 2)).Select
Selection.Copy
Workbooks.Add
Range("A1").PasteSpecial
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\kentan\Desktop\Managed Fund" & cells(i,1) & ".xls"
ActiveWorkbook.Close
Else
i = i + 1
End If
Loop
Application.CutCopyMode = False
End Sub
Given the repetitious action of adding multiple workbooks, I would shovel that operation to a 'helper' sub.
Option Explicit
Public Const strSA As String = "C:\Users\kentan\Desktop\Managed Fund "
Sub iris()
Dim i As Long
With ActiveSheet
With .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp).Offset(0, 1))
.Sort key1:=.Columns(1), order1:=xlAscending , _
key2:=.Columns(2), order2:=xlAscending , _
Header:=xlYes, MatchCase:=False, _
Orientation:=xlTopToBottom, SortMethod:=xlStroke
End With
For i = 2 To .Rows.Count
If LCase(.Cells(i, "A").Value2) = LCase(.Cells(i - 1, "A").Value2) And _
LCase(.Cells(i, "A").Value2) <> LCase(.Cells(i + 1, "A").Value2) Then
newiris .Cells(i, "A").Value2, .Cells(i, "B").Value2
End If
Next i
End With
End Sub
Sub newiris(nm As String, nfo As String)
Application.DisplayAlerts = false
With Workbooks.Add
Do While .Worksheets.Count > 1: .Worksheets(2).Delete: Loop
.Worksheets(1).Cells(1, "A").Resize(1, 2) = Array(nm, nfo)
.SaveAs filename:=strSA & nm, FileFormat:=xlOpenXMLWorkbook
.Close savechanges:=False
End With
Application.DisplayAlerts = true
End Sub

Excel - A macro to group by column & sum values for big Range (15K rows)

I have an excel sheet with 2 columns and can have upto 15K rows. I need to sum values, group by first and second column. Currently I am using the followinn macro, the code is copying the data across a new sheet, sorting it and removing the duplicates while adding the count if a match found. I have tested it for 500 rows items to so far and it takes couple of minutes and I am worried of the time taken if there are more rows (as there can be up to 15K rows).
Sub consolidateData()
Dim lRow As Long
Dim ItemRow1, ItemRow2 As String
Dim lengthRow1, lengthRow2 As String
Columns("A:C").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A1").Select
ActiveSheet.Paste
Cells.Select
Selection.Sort _
Key1:=Range("A2"), Order1:=xlAscending, _
Key2:=Range("C2"), Order2:=xlDescending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
lRow = 2
Do While (Cells(lRow, 1) <> "")
ItemRow1 = Cells(lRow, "A")
ItemRow2 = Cells(lRow + 1, "A")
lengthRow1 = Cells(lRow, "C")
lengthRow2 = Cells(lRow + 1, "C")
If ((ItemRow1 = ItemRow2) And (lengthRow1 = lengthRow2)) Then
Cells(lRow, "B") = Cells(lRow, "B") + Cells(lRow + 1, "B")
Rows(lRow + 1).Delete
Else
lRow = lRow + 1
End If
Loop
End Sub
Could you please suggest if there is a quickest way to do it.
Thanks in Advance.
Thera are a few things you would do to improve your performance:
There is a RemoveDuplica method you could use, as of SOF Delete all duplicate row:
Sub DeleteRows()
With ActiveSheet
Set Rng = Range("A1", Range("B1").End(xlDown))
Rng.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
End With
End Sub
If you use Preformated table it will be easy to fill up a new sheet with the information you need
When apropriate, always use the code below to improve your funcion/sub performance:
Application.ScreenUpdating = False
Might be better if you copy only the columns that should be grouped by, then you do the sumif into the value column.
Hope it was helpful.
This is a quick way to have your macro faster. It would stop animation and a few other perks. :)
However, it would be a great idea to rebuild your code from the beginning, avoinding the selects.
Sub consolidateData()
Dim lRow As Long
Dim ItemRow1, ItemRow2 As String
Dim lengthRow1, lengthRow2 As String
call onstart
Columns("A:C").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A1").Select
ActiveSheet.Paste
Cells.Select
Selection.Sort _
Key1:=Range("A2"), Order1:=xlAscending, _
Key2:=Range("C2"), Order2:=xlDescending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
lRow = 2
Do While (Cells(lRow, 1) <> "")
ItemRow1 = Cells(lRow, "A")
ItemRow2 = Cells(lRow + 1, "A")
lengthRow1 = Cells(lRow, "C")
lengthRow2 = Cells(lRow + 1, "C")
If ((ItemRow1 = ItemRow2) And (lengthRow1 = lengthRow2)) Then
Cells(lRow, "B") = Cells(lRow, "B") + Cells(lRow + 1, "B")
Rows(lRow + 1).Delete
Else
lRow = lRow + 1
End If
Loop
call onende
End Sub
Public Sub OnEnd()
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
ThisWorkbook.Date1904 = False
Application.StatusBar = False
End Sub
Public Sub OnStart()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.Calculation = xlAutomatic
ThisWorkbook.Date1904 = False
ActiveWindow.View = xlNormalView
End Sub

copy/paste a range of data from non contiguous to non-contiguous columns in a different worksheet based on condition

So following the prious question (VBA migrating data from different worksheets to one worksheet at specific locations) I have edited the code to the following below based on posts from other code researchers/experts.
The previous codes (see link) were working up to a certain point where a running time error would come up. I have followed the suggestions and removed .Select. and .Activate from the copy/paste operations however currently the code below does not do anything from the point 'copy from feedstock records sheet' onwards. I am sure I am doing something wrong or that I could approach my problem in a different way but I am struggling to find a solution. Does anyone have any ideas?
After debugging I have managed to overcome the error 13 which was related to cells though defined as date the order of date was messed up and once I changed the order of the cells it was ok. However I know have the error 1004 as decribed in the comments below (see my last comment). I was wondering if anyone has any approche on how to solve this issue. I have marked where the error appears (it's on the second loop). in sht5 the date only starts on 01/01/2015 however sht4 starts on 07/08/2014. After I fixed the problem on the first days in 2014 the code was able to run until it reached the value 01/01/2015 when past special the range specified in bold below. Could anyone help? Thanks
Option Explicit
Sub main()
'open/close worksheets from huddle folder and teamviewer'
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim Wb3 As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Dim sht4 As Worksheet
Dim sht5 As Worksheet
Dim i As Long, j As Long, k As Long, lastrow1 As Long, lastrow2 As Long, lastrow3 As Long
Dim monthsi As Date, monthsk As Date, monthsj As Date
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
Set Wb1 = Workbooks.Open("U:\Data from plants\Huddle\EEL Feedstock Records - NEW VERSION.xlsx")
Set Wb2 = Workbooks.Open("U:\Data from plants\Teamviewer\EE.xlsx")
Set Wb3 = ThisWorkbook
Set sht1 = Wb1.Sheets("Feedstock Usage (Non-beet site)")
Set sht2 = Wb2.Sheets("Sheet1")
Set sht3 = Wb3.Sheets("Feedstock records")
Set sht4 = Wb3.Sheets("Teamviewer")
Set sht5 = Wb3.Sheets("Plants data")
sht3.Cells.Delete Shift:=xlUp
sht4.Cells.Delete Shift:=xlUp
sht1.Cells.Copy
sht3.Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
Wb1.Close False
sht2.Cells.Copy
sht4.Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
Wb2.Close False
'copy from feedstock records sheet'
lastrow1 = sht3.Range("C" & Rows.Count).End(xlUp).Row
i = 10
lastrow2 = sht4.Range("A" & Rows.Count).End(xlUp).Row
k = 4
lastrow3 = sht5.Range("A" & Rows.Count).End(xlUp).Row
j = 5
Do
monthsi = sht3.Cells(i, "C").Value
If sht5.Cells(j, "A").Value = monthsi Then
sht3.Range(Cells(i, "D"), Cells(i, "E")).Copy
sht5.Range(Cells(j, "VE"), Cells(j, "VF")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "G"), Cells(i, "H")).Copy
sht5.Range(Cells(j, "VI"), Cells(j, "VJ")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "J"), Cells(i, "K")).Copy
sht5.Range(Cells(j, "VM"), Cells(j, "VN")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "M"), Cells(i, "N")).Copy
sht5.Range(Cells(j, "VY"), Cells(j, "VZ")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "P"), Cells(i, "Q")).Copy
sht5.Range(Cells(j, "VQ"), Cells(j, "VR")).PasteSpecial xlPasteValues
End If
i = i + 1
Loop Until i = lastrow1 + 1
Do
monthsk = sht4.Cells(k, "A").Value
If sht5.Cells(j, "A").Value = monthsk Then
sht4.Cells(k, "H").Copy
sht5.Cells(j, "XW").PasteSpecial xlPasteValues
sht4.Cells(k, "I").Copy
sht5.Cells(j, "YJ").PasteSpecial xlPasteValues
sht4.Range(Cells(k, "J"), Cells(k, "O")).Copy
**sht5.Range(Cells(j, "ZK"), Cells(j, "ZP")).PasteSpecial xlPasteValues**
sht4.Cells(k, "U").Copy
sht5.Cells(j, "XU").PasteSpecial xlPasteValues
sht4.Cells(k, "X").Copy
sht5.Cells(j, "XV").PasteSpecial xlPasteValues
sht4.Cells(k, "Y").Copy
sht5.Cells(j, "YH").PasteSpecial xlPasteValues
sht4.Cells(k, "AB").Copy
sht5.Cells(j, "YI").PasteSpecial xlPasteValues
sht4.Range(Cells(k, "AN"), Cells(i, "AP")).Copy
sht5.Range(Cells(j, "XR"), Cells(j, "XT")).PasteSpecial xlPasteValues
sht4.Cells(k, "AQ").Copy
sht5.Cells(j, "XQ").PasteSpecial xlPasteValues
End If
k = k + 1
Loop Until k = lastrow2 + 1
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub
It looks like you may want to bring the setting of monthsi, monthsj and monthsk inside your loops. You increase i in the first loop for example, but that doesn't change monthsi, so if the comparison is false to begin with, the if statement will never run.
For example, the first loop would become:
Do
monthsi = sht3.Cells(i, "C").Value
If monthsj = monthsi Then
sht3.Range(Cells(i, "D"), Cells(i, "E")).Copy
sht5.Range(Cells(j, "VA"), Cells(j, "VB")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "G"), Cells(i, "H")).Copy
sht5.Range(Cells(j, "VE"), Cells(j, "VF")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "J"), Cells(i, "K")).Copy
sht5.Range(Cells(j, "VI"), Cells(j, "VJ")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "M"), Cells(i, "N")).Copy
sht5.Range(Cells(j, "VM"), Cells(j, "VN")).PasteSpecial xlPasteValues
sht3.Range(Cells(i, "P"), Cells(i, "Q")).Copy
sht5.Range(Cells(j, "VQ"), Cells(j, "VR")).PasteSpecial xlPasteValues
End If
i = i + 1
Loop Until i = lastrow1 + 1 Or j = lastrow3 + 1
This still leaves the question raised by PartyHatPanda of why you are checking against j to end the loop when j isn't changing, so there may be a deeper error in your logic. ie if j should be increasing as well, then the assignment of monthsj should also be brought into the loop in the same way.

Change copy-paste VBA macro from line-by-line to batch copy-paste

I currently have a VB macro that will copy-past values from one sheet to another. Currently however, the VB is written in a way that it will do it row-by-row and this runs pretty slow since it goes through a few thousand rows. I'm wondering how would be best to change my VB to do a batch copy-paste to cut down on waiting time. Code is:
Sub copypaste_settlement_rows()
Dim LastRow As Long
Application.ScreenUpdating = False
Sheets("Settlement Template").Select
'find last row in column A
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For x = 2 To LastRow
Cells(x, 1).Resize(1, 42).Copy
Sheets("PIVOT DATA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Settlement Template").Select
Next x
Sheets(">> START HERE <<").Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
This should be instantaneous and it does not use the clipboard:
Sub copypaste_settlement_rows()
Dim v
With Sheets("Settlement Template")
v = .Cells(2, 1).Resize(.Cells(.Rows.Count, "A").End(xlUp).Row, 42)
End With
With Sheets("PIVOT DATA")
.Cells(.Rows.Count, "A").End(xlUp).Resize(UBound(v), UBound(v, 2)) = v
End With
End Sub
An extremely simple way (and the fastest I've seen in my own code) is:
ThisWorkbook.Worksheets("PIVOT DATA").Range("A2:A" & lastRow) = ThisWorkbook.Worksheets("Settlement Template").Range("A2:A" & lastRow).Value