Excel VBA: Copying/Pasting Range - vba

I'm writing a macro that opens a number of different workbooks, copies data from each, and compiles into a single "master" workbook. In the below code example, wb2 is one of the workbooks and I'm copying from, and wb1 is the master.
lrow3A is the last row of data in the source workbook. Lrow3 is the last row of data in the master workbook.
lrow3A = wb2.Sheets("DCF3").Cells(1048576, 2).End(xlUp).Row
wb2.Sheets("DCF3").Range(Cells(6, 1), Cells(lrow3A, 16)).Copy _
Destination:=wb2.Worksheets("DCF3").Cells(lrow3 + 1, 2)
I'm getting a "Subscript out of range" error on the copy line.

I think you should code:
With wb2.Sheets("DCF3") 'reference "source" worksheet
lrow3A = .Cells(.Rows.Count, 2).End(xlUp).Row ' get referenced sheet column "B" last not empty cell row index
.Range("A6:P" & lrow3A).Copy _
Destination:=wb1.Worksheets("DCF3").Cells(lrow3 + 1, 2) 'copy referenced sheet range in columns A:P from row 6 to row 'lrow3A' and paste it to "master" workbook sheet "DCF3" starting from its column B cell at row 'lrow3'+1
End With

Related

Excel vba - paste below header

I need to copy a column from one workbook to another.
y.Sheets("data").Range("A2:A1048576").Clear 'delete contents of target columns first
x.Sheets("file").Range("C:C").Copy 'copy column from another sheet
y.Sheets("data").Range("A1").End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
I'm trying to paste it to starting from A2 since A1 is my header.
I'm getting a application-defined or object-defined error
It's best practice not to reference entire columns (some million cells) when you don't need to:
With y.Sheets("data") ' reference sheet "data" in workbook 'y'
.Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).ClearContents ' clear 'referenced sheet column "A" cells from row 2 down to last not empty one
End With
With x.Sheets("file") ' reference sheet "file" in workbook 'x'
With .Range("C1", .Cells(.Rows.Count, "C").End(xlUp)) 'reference referenced sheet column "C" cells from row 1 down to last not empty one
y.Sheets("data").Range("A2").Resize(.Rows.Count).Value = .Value ' paste referenced range values to workbook "y" sheet "data" starting from cell A2
End With
End With
Or the following
With y.Sheets("data")
Intersect(.UsedRange, .Columns("A")).Clear
Intersect(x.Sheets("file").UsedRange, x.Sheets("file").Columns("C")).Copy .Range("A2")
End With
The issue here is you copy a complete column Range("C:C").Copy and that means you have to paste to a complete column otherwise it would not fit in and exceed Excel's maximum row count.
Therefore I suggest to copy only that part of column C that contains data:
With x.Sheets("file")
.Range("C1", .Cells(.Rows.Count, "C").End(xlUp)).Copy
End With
This copies only from C1 to the last used cell. And then you can paste it directly to A2
y.Sheets("data").Range("A2").PasteSpecial xlPasteValues

Copy multiple columns of values from one workbook to corresponding sheets on another workbook

I have been trying to get this to work, but I am not sure how to get it to work, any help would be appreciated.
I have 2 workbooks, workbook 1 has multiple sheets, each one labelled with a different name. Workbook 2 has one summary sheet with a column of values for each individual.
What I am trying to achieve is:
on workbook 1 check the sheet name
switch to workbook 2 and find the column with the same name. All the names are on row 6, from column I to DD. Also, each name is in 2 cells merged together, I don't know if this affects it.
Once the name on row 6 is found, I want it to go down 6 cells, and copy the value.
switch back to workbook 1 and paste it into cell B37.
Repeat this process but this time go down 7 cells, copy the value and paste it into cell B102 OF Workbook 1. I have about 30 cells to copy and past like that.
once complete repeat everything again for the next sheet on workbook 1.
Another Important issue is that, not all sheet names on workbook 1 exists on workbook 2, I have a feeling the below code will throw an error as soon as it doesn't find a match. So I would like to be able to skip the sheets on workbook 1 that it doesn't find a matching name for on workbook 2 summary sheet.
I have put the code I have below, but I keep getting the error "Method or data member not found"
Sub Measures()
Dim wb1 As Workbook
Dim Sht As Worksheet
Dim Rng, Rng2 As Range
Dim wb2 As Workbook
Dim cell As Range
Dim ws As Worksheet
Set wb1 = ThisWorkbook
Set wb2 = Workbooks("November Stream 1.xlsm")
Set Sht = wb1.Worksheets("Summary")
Set Rng = Sht.Range("A6:A" & Sht.Cells(Sht.Column.Count, "A").End(xlUp).Column)
For Each cell In Rng
Set ws = wb2.Sheets(cell.Text)
ws.Range("B37").Value = cell.Offset(6, 0).Value
ws.Range("B102").Value = cell.Offset(7, 0).Value
Next cell
End Sub
Thank you for any help!

Copy/paste data into consolidated list

I'm stuck on how to structure a piece of code that:
Loops through all worksheets that begin with the number 673: (e.g. 673:green, 673:blue)
Selects the data in these worksheets from row 5 up until the last row with data - code that works for this (generously provided by another user) is
Dim report As Worksheet
Set report = Excel.ActiveSheet
With report
.Range(.Cells(5, "K"), .Cells(.Rows.Count, "K").End(xlUp)).EntireRow.Select
End With
Select the "Colours" worksheet
Paste the rows at the next available blank row. There could be up to 40/50 worksheets which will have data pasted into the "Colours" worksheet so I need the data added to the next available line.
Thank you in advance.
Loop over the sheets in the workbook and check their names
For Each sheet in ActiveWorkbook.Worksheets
If Instr(sheet.Name,"673")>0 Then
...
End If
Next
Good, but you're going to want to copy.
Selection.Copy
Just select.
Worksheets("Colours").Select
Find the last row then go to the next. The row is found by finding the first populated row from the bottom up. Note I used explicit sheet references, which is unnecessary since you selected the sheet already. This is better form, however, if you will be manipulating data on multiple sheets in your code.
lastRow = Worksheets("Colours").Cells(Worksheets("Colours").rows.count,1).End(xlUp).Row
Worksheets("Colours").Cells(lastRow + 1, 1).Select
Activesheet.Paste

If The cell has values, select the same quantity of lines in another Sheet

In my first sheet I insert 389 lines of text, it makes a process and appears in sheet 3 fully processed, but I need to select the same number of lines in the Sheet 3, then copy it.
The main problem is that the quantity of lines that I insert in the first sheet can be from 1 to 10,000.
If you are inputing your data such that each line is filled from the first line down to some line you can find the last row number with the following, where "A" is the column the data is in.
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Then you can go through the data with a loop like:
For each cell in sht.Range("A1:A" & lastrow)
'do something with cell
next cell

Excel VBA Copy multiple sheets into one sheet not working

I have the following vba:
Data is all text, Rows A - J with column headers on every sheet are the same
Data is sql queries all with "top 1000"
4 sheets (sheet1, sheet2, sheet3, Master)
sheet 1: 100 rows
sheet 2: 34 rows
sheet 3: 900 rows
Master: merged data from 3 sheets
PROBLEM: Sheet3 only copies 84 rows specifically however adding more rows to other sheets will copy over to Master. Only sheet3 will not copy more than 84 rows.
' Step 1: Clear master before updating
' Step 2 : Loop through the regional sheets
Sub Consolidate()
Dim cell As Range
Dim wks As Worksheet
Sheets("Master").Range("A2:Z65536").ClearContents
For Each wks In ThisWorkbook.Worksheets
If wks.Name <> "Master" And wks.Range("A2") <> "" Then
For Each cell In wks.Range(wks.Range("A2"), wks.Range("A2").End(xlDown))
cell.EntireRow.Copy Destination:=Worksheets("Master").Range("A65536").End(xlUp).Offset(1, 0)
Next cell
End If
Next wks
End Sub
Is the data starting in Range(A2) always populated?
The For Each cell In wks.Range(wks.Range("A2"), wks.Range("A2").End(xlDown)) will start from A2 and go to the last populated cell before a blank/empty cell.