Creating a Loop Through 4 worksheets Paste not working - excel

Hi guys I'm doing a course for Udemy and the lecturer unfortunately has not been the most responsive.
I have a workbook called QuarterlyReport and 5 sheets.
East Records
West Records
North Records
South Records
Yearly Report
My code formats the Worksheets 1-4 and then copy pastes the information to Yearly Report on the last unused row. For some reason, the code is only pasting South Records. My goal is to copy every single sheet 1 - 4 and paste it onto the fifth sheet "YEARLY REPORT".
Public Sub Finalreportloop()
Dim i As Integer
i = 1
Do While i <= Worksheets.Count - 1
Worksheets(i).Select
AddHeaders
FormatData
AutoSum
' copy the current data
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Copy
' select the final report WS'
Worksheets("yearly report").Select
'find the empty cells
LastRow = Sheets(i).Range("A" & Sheets(i).Rows.Count).End(xlUp).Row
'paste the new data in
ActiveSheet.Paste
i = i + 1
Loop
End Sub
The Addheaders, FormatData and AutoSum are in reference to other Modules I've created. Thank you everyone!

The code determines the last row of the sheet that you are copying from, but you don't do anything with that information. Instead, it just pastes into the active sheet and overwrites the data posted in the last loop instance. Hence it looks as if it is only copying/pasting the last data set.
You need to find the last row in the Yearly sheet, then paste the data below that.

You coudld try one of the methods below:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim wsLastRow As Long, wsLastColumn As Long, yrLastRow As Long
Dim rngCopy As Range
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "YEARLY REPORT" Then
With ws
'Method 1 - You can siply used range
' .UsedRange.Copy
'Method 2 - You can calculate LastColumn & LastRow and create the range
wsLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Find last row of column A.
wsLastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column '<- Find the last column of row 1.
Set rngCopy = .Range(Cells(1, 1), Cells(wsLastRow, wsLastColumn)) '<- Create the range to be copy.
rngCopy.Copy
End With
With ThisWorkbook.Worksheets("YEARLY REPORT")
yrLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Find last row of column A.
.Range("A" & yrLastRow + 1).PasteSpecial xlPasteValues
End With
End If
Application.CutCopyMode = False
Next ws
End Sub

Related

Select date in P1 and copy to the the rest of the cells in P Column

Pretty much what I listed in the subject line with the exception this needs to only add data to column P if there is data in adjacent rows.
For example if there are 20 rows of data I only need this to copy P1 through P20. But the rows will fluctuate, sometimes there may be 50 rows, sometimes there may be 5 etc....
Sub DaysToPay()
'DaysToPay Macro
Range("P2").Select
ActiveCell.FormulaR1C1 = "=RC[-5]+10"
Range("P3").Select
'...
End Sub
Sub Free()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long 'last row
lr = ws.Range("P" & ws.Rows.Count).End(xlUp).Row
ws.Range("P1:P" & lr).Copy '<-- Copy
ws.Range("?").PasteSpecial xlPasteValues '<-- Paste where?
End Sub

Merge ranges in one column

I have the following script copying a range "F30:F37", "G30:G37" from Sheet 1. I am trying to select both ranges into Sheet2 starting with ROW G101. However, only Sheet 1 "G30:G37" data copies into Sheet 2, ROW G101. What could be the issue, would you be able to simplify my data pull? Listed below, Macro:
Dim LastRow As Long
Dim SHEET2 As Worksheet
Set Results = Sheets("SHEET2")
LastRow = Results.Cells(Results.Rows.Count, "Z").End(xlUp).Row
Range("F30:F37").Copy
Results.Range("G" & LastRow + 101).PasteSpecial xlPasteValues
Range("G30:G37").Copy
Results.Range("G" & LastRow + 101).PasteSpecial xlPasteValues
Application.DataEntryMode = False
End Sub
This is how column with rows display, please note, row G has no header:
This would be my result:
My result would look like the second image, sheet 2
First off, Results is a variable that should be in your Dim statement, not "Sheet2". Secondly, you are pasting over the first paste. If you want row 101 for the first, paste in row 101. Then find the last row and paste the info below. And lastly, you want Application.CutCopyMode to take you out of the copy/paste.
Sub CopyData()
Dim LastRow As Long
Dim Results As Worksheet
Set Results = Sheets("SHEET2")
Range("F30:F37").Copy
Results.Range("G101").PasteSpecial xlPasteValues
LastRow = Results.Cells(Results.Rows.Count, "G").End(xlUp).Row
Range("G30:G37").Copy
Results.Range("G" & LastRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub

Excel copy and paste rows using loop

my questing is about trying to copy and paste 4 rows (4-7) under each row for the entire sheet, maybe using loop. I have pasted the copied rows under row 8 & 13 as an example (I would like to be able to do that for the remaining sheet until the rows are empty). Your expertise is greatly appreciated.
enter image description here
To implement:
Create a second sheet (in my code, referenced as Sheet2)
Paste the rows on this sheet that are to be inserted on the first sheet
Run Macro
Option Explicit
Sub EnterRows()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update
Dim i As Long, LR As Long, myRange As Range
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set myRange = ThisWorkbook.Sheets("Sheet2").Range("1:4")
Application.ScreenUpdating = False
For i = LR To 2 Step -1
myRange.Copy
ws.Range("A" & i + 1).Insert xlDown
Next i
Application.ScreenUpdating = True
End Sub

Excel formula only bring over row in other worksheet if cell in column A is not blank

I have two worksheets in one Excel workbook, and I only want to take the lines that have data in the cell (from worksheet1 into worksheet2) if Column A has data in it. My formula in worksheet 2 is =IF('Raw Data'!A2<>"", 'Raw Data'!A2,), but I actually don't want it to bring in the row at all if there is no data as shown in Rows 3 and 5. Right now it is bringing the whole row in:
In
you see that it is still bringing the row into worksheet 2 if there is no data. Any ideas how to only bring in the rows with the data?
Sub DataInCell()
Dim rw As Long
rw = 2
' Select initial sheet to copy from
Sheets("Raw Data").Select
' Find the last row of data - xlUp will check from the bottom of the spreadsheet up.
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' For loop through each row
For x = 2 To FinalRow
If Cells(x, 1).Value <> 0 Then
Range("A" & x & ":C" & x).Copy
Sheets("Sheet1").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1 'Continue incrementing through the rows.
Cells(NextRow, 1).Select ' Find the next row.
ActiveSheet.Cells(NextRow, "A").PasteSpecial xlPasteAll ' Paste information.
Sheets("Raw Data").Select 'Reselect sheet to copy from. Probably uneccessary.
End If
Next x
End Sub
After you update the sheet names on the 3rd and 4th line, you will see that the code carries over the entire row. You can modify using Range(Cells, Cells) if you want partial ranges.
Option Explicit
Sub Non_Blanks()
Dim ms As Worksheet: Set ms = ThisWorkbook.Sheets("Sheet1") '<-- Master Sheet
Dim ns As Worksheet: Set ns = ThisWorkbook.Sheets("Sheet2") '<-- New Sheet
Dim i As Long, MoveMe As Range, LR As Long
For i = 2 To ms.Range("B" & ms.Rows.Count).End(xlUp).Row
If ms.Range("A" & i) = "*" Then
If Not MoveMe Is Nothing Then
Set MoveMe = Union(MoveMe, ms.Range("A" & i))
Else
Set MoveMe = ms.Range("A" & i)
End If
End If
Next i
If Not MoveMe Is Nothing Then
LR = ns.Range("A" & ns.Rows.Count).End(xlUp).Offset(1).Row
MoveMe.EntireRow.Copy
ns.Range("A" & LR).PasteSpecial xlPasteValuesAndNumberFormats
End If
End Sub

Copy specific rows based on a condition in another cell

I am trying to copy certain cells if the word "FLAG" is a cell in that same row.
For example, I have data in excel like the following:
So if the word Flag is in any of the cells I want to copy the Description, Identifier and Final Maturity columns (Columns A-C) as well as the corresponding date column. So for the first row (AA) under Jan/Feb there is the word Flag. I would want to copy over columns A-E to another worksheet or table.
I would like to use a VBA but I am not sure how
The following code will do what you expect, each time it finds the word FLAG, the first 3 cells will be copied as well as the value for the given month will be copied to a new row, and if a second flag is found that will be copied to the next available row:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
Dim wsResult As Worksheet: Set wsResult = Sheets("Sheet2")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 2 To LastRow 'loop through rows
For x = 15 To 23 'loop through columns
If ws.Cells(i, x) = "FLAG" Then 'if FLAG found in column
NextFreeRow = wsResult.Cells(wsResult.Rows.Count, "A").End(xlUp).Row + 1 'get the next empty row of your wsResult sheet
ws.Range("A" & i & ":C" & i).Copy 'copy first three cells in given row
wsResult.Range("A" & NextFreeRow).PasteSpecial xlPasteAll 'paste into your Result sheet
ws.Cells(i, x - 11).Copy 'copy the value for which there was a flag
wsResult.Cells(NextFreeRow, 4).PasteSpecial xlPasteAll 'paste in the fourth cell in the sheet wsResult
End If
Next x
Next i
End Sub

Resources