VBA copy range of rows - vba

Is it possible to copy range of rows and paste those into other sheet?
I've tried this but i get 1004 error:
.Rows(i_cls_frst_row, i_cls_last_row).Copy
Regards
Michał

Is this what you are trying?
If you have non contiguous rows like i_cls_frst_row=1 and i_cls_last_row=3 and you are trying to copy two rows then try this
Range(i_cls_frst_row & ":" & i_cls_frst_row & "," & _
i_cls_last_row & ":" & i_cls_last_row).Copy
If you are trying to copy from i_cls_frst_row to i_cls_last_row then try this
Rows(i_cls_frst_row & ":" & i_cls_last_row).Copy

Yes, change your line
.Rows(i_cls_frst_row, i_cls_last_row).Copy
to:
.Rows(i_cls_frst_row & ":" & i_cls_last_row).Copy
Then later on you can paste it where you need

Use a range of rows:
Range(Rows(i_cls_frst_row), Rows(i_cls_last_row)).Copy
Or a Range's EntireRow property:
Range(Cells(i_cls_frst_row, 1), Cells(i_cls_last_row, 1)).EntireRow.Copy
You can also use a Range Resize, if you know the number or rows you wish to copy:
Rows(i_cls_frst_row).Resize(2).copy
(this copies 2 rows)

You can define a Range object and select all the required rows/row as a Range
Dim example As Range
Set example = Range("A1:E1")
example.Select
Selection.Copy

Related

VBA - Copy / Paste to multiple columns with variable row start (array)

I'd like to shorten my code by putting multiple columns to one command.
I'm copying formulas from one row but specified columns and want to paste to another sheet in the same wksht. Just the row number is a variable. Maybe do this by an array?
As an example I'm pasting below not working code with only 3 columns.
Sub Copy()
Dim LastrowQ As Long
Dim LastrowR As Long
LastrowQ = Cells(Rows.Count, "B").End(xlUp).Row
LastrowR = LastrowQ + 1
Workbooks(Rep1).Worksheets("Formula").Range("A2, C2:F2, N2, V2:AD2").Copy
*Workbooks(Rep1).Worksheets("Report").Range("A" & LastrowR, "C" & LastrowR & ":F" & LastrowR, "N" & LastrowR, "V" & LastrowR & ":AD" & LastrowR).PasteSpecial Paste:=xlPasteFormulas*
End Sub
Could you please help to modify the code in paste part as it doesn't work right now? This would allow to make original code much shorter and hopefully faster.
What if I'd like to propagate function in second sheet from variable row till the specified row at the same time? ("A" & LastrowR & ":A" & LastrowS,...)
Thanks in advance

CountIf referencing named range and starting on row 3

I am trying to do a countif formula in a cell on sheet 'B'. So far my formula is:
=COUNTIF('Consol List'!R[-3]C[-3]:R[-1]C[-3],""<>0"")-COUNTBLANK('Consol List'!R[-3]C[-3]:R[-1]C[-3])
however, I need the two Consol List ranges to reference a named column of LastColumn and the row to start at 3 and down to LR.
For some reason I can not figure out how to do this. help is appreciated.
the only issue is it is giving an application defined or object defined and I’m not sure why..?
"=COUNTIF('Consol List'!" & Range(Cells(3, LastColumn), Cells(LR, LastColumn)).Address(0, 0) & """<>0"")-COUNTBLANK('Consol List'!" & Range(Cells(3, LastColumn), Cells(LR, LastColumn)).Address(0, 0) & ")"
use A1 notation:
"=COUNTIF('Consol List'!" & Range(Cells(3,LastColumn),Cells(LR,LastColumn)).Address(0,0) & ",""<>0"")-COUNTBLANK('Consol List'!" & Range(Cells(3,LastColumn),Cells(LR,LastColumn)).Address(0,0) & ")"

Copy data up to last row and column

I am trying to copy all the data from multiple workbooks to a single workbook and distribute them to different worksheets.
How should I change the code below to automatically select the data up to the last row and column with value and copy them instead of declaring the range?
For i = 16 To ws.Range("I" & Rows.Count).End(xlUp).Row 'Sheet1 is MasterSheet
File = ws.Range("C" & i) & ws.Range("I" & i) 'File Location and Excel Name
Copy = ws.Range("U" & i) & ":" & ws.Range("V" & i) 'Range to be copied
Workbooks.Open File, 0, 1 'Open File as Read Only
Range(Copy).Copy
ThisWorkbook.Sheets(ws.Range("W" & i).Value).Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial 12 'Paste as Values only
ActiveWorkbook.Close False 'Close WorkBook without saving
Selection.Value = Selection.FormulaR1C1 'Force F2 and Enter selected range
Range("A1").Select
Next i
Your solution works fine, Paolo. I did quite a bit of research on the topic some time ago and I found out these ways are the fastest and simplest ones. I don't think Excel is very good with these things, so I have always tried to stick to the basics.
Just two comments:
I would not use .UsedRange.Copy, since it will return the range of cells that have ever been used.
I would also try to avoid using.select as much as possible. You can find examples on how to avoid it in
How to avoid using Select in Excel VBA

Application.VLookup function always returns #N/A

I am trying to use the Application.VLookup function in Visual Basic to find a value in a different workbook. However, whenever I use it, it always returns #N/A.
This is the layout of my function. LastRow() just returns the row number of the last row. SHORTAGE_SBT is a variable containing the source workbook name. SBT_Last is the last row of SHORTAGE_SBT. The ID that I'm searching with in in the B column, hence why I use "B" & ind to refer to it.
For ind = 4 To LastRow()
Range("H" & ind).Select
ActiveCell.Value = Application.VLookup("B" & ind, Workbooks(SHORTAGE_SBT).Sheets(1).Range("A14:DZ" & SBT_Last), Range("DZ1").Column, False)
Next
I have tried recording a macro for VLookup to see if it would help me understand the problem. The macro gave me this function, which worked but could not be used because it contains the hardcoded file name instead of using the variable.
Range("H" & ind).FormulaR1C1 = "=VLOOKUP(RC[-6],'[filename.xls]Sheet1'!R14C1:R2382C130, COLUMN(R[-3]C[122]), FALSE)"
I cannot see any significant difference between the way the macro lays out the arguments of the function as opposed to mine, other than using more direct references. I have tried using direct numbers in my code but doing so hasn't helped either.
Application.VLookup("B" & ind, Workbooks(SHORTAGE_SBT).Sheets(1).Range("A14:DZ" & SBT_Last), Range("DZ1").Column, False)
"B" & ind will be searched for "literally"; it will not be transformed into a range address because it is interpreted here by VBA, not by Excel. Try:
Application.VLookup(Range("B" & ind), Workbooks(SHORTAGE_SBT).Sheets(1).Range("A14:DZ" & SBT_Last), Range("DZ1").Column, False)
' ^^^^^^^^^^^^^^^^
Combine the two approaches:
Range("H" & ind).FormulaR1C1 = _
& "=VLOOKUP(RC[-6],'[" & Workbooks(SHORTAGE_SBT) _
& "]Sheet1'!R14C1:R2382C130, COLUMN(R[-3]C[122]), FALSE)"

putting a string from a cell into the middle of my index-match VBa script

I am trying to use the index-match formula to reorganize data such that all of the names in column J that have a matching value in column A will be placed in the same spot. I'm going to do this for 5 different columns so that the 5 names on a team will be in the same row as the name of the corresponding client.
My issue is that the index-match formula needs to be able to dynamically shorten or lengthen the size the arrays it uses based on how many clients there are when the VBA script is run.
I can dynamically determine what numbers I need in the formula using COUNTA, but the code will not compile when I try to put it in my formula. My formula is below
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & ",MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0)"
As you can see I need the strings in cells J3 and J1 to be used as the arrays for the index match. J3 = $J$2:$J$2369 and J1 = $A$2:$A$1113
When I run the code it gives me a "Application-Defined or Object-defined error."
You need to use the Range member of worksheet
so use 'test sheet two'!Range("J2:J2369") rather than 'test sheet two'!("J2:J2369").
The following runs
ActiveCell.Formula = _
"=INDEX('test sheet two'!Range(""" & Range("J3") & """) _
,MATCH(Sheet1!A5,'test sheet two'!Range(""" & Range("J1") & """),0))"
Your formula was not including the column criteria for the INDEX Function.
Try:
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & "," & _
"MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0), 1)"
Notice the additional , 1)" on the end of the formula.
Also, you do not have to first Select the cell which you want to enter the formula in, you could just use:
Range("B7").Formula =