VBA code for copy until last row - excel

How can I modify
XLApp.Range("A1:K1" & LastRow).Copy
if I want to copy the A1:K1 untill the last row there is data in one of the cells in the selected area . Sorry for my poor English.

LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
will give you the last row in column no 1 ("A"). For last row in column k, you need to use 11 in place of 1.

The code below shows a generic way to do that:
Range(Range("A1", Range("A1").End(xlToRight)), Range("A1", Range("A1").End(xlDown))).Rows.Count
you can change the A1 reference to other if you want. The codes copy a range the starts from A1 to right and from right to down.

Related

finding last row of highlighted cell

How do i find what the last row of of a column that is highlighted?
Currently I only know how to find last row that is used.
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
You are not 100% correct with your statement - with your current code you will get the row number of the last row of column A (that's what the 1 in your statement stands for) - not neccessarily the last row in use of the whole sheet.
If you want to know the last row of a specific column, just change this 1 to the column number you are interested. Probably with "highlighted" you mean the active cell, so that would be
LastRow = Cells(Rows.Count, Activecell.Column).End(xlUp).Row
A rather complete discussion about how to get the last row/column/cell can be found at Find last used cell in Excel VBA
If I have understood your requirement correctly then you will need an approach like this.
lngLastRow = Selection.Cells(Selection.Cells.Count, 1).Row

Conceptual understanding of xlDown

We find last row using code line
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
I am refreshing my conceptual understanding of xlDown. I refer to data in column B in Sheet1 as appended below.
My understaqnding of End(xlDown) has been that it searches from the top down finding the last used cell before a blank cell.Its concept can be simulated by pressing ctrl down. If we highlight a column that contains data intermixed with blanks and then press ctrl down - it will go to the cell before the first blank cell. Pressing ctrl down in Column B Sheet 1 takes to B2 and then to B18. But if I use the following code snippet I get the last row as 1048576.
What logic this code snippet follows?
Can anyone explain it please to clarify this trivial issue.
You will get the same result if you remove .End():
lastRow = .Cells(.Rows.Count, "B").Row
This is because ws.Cells(ws.Rows.Count, "B") is already the last possible cell of the B column. Calling .End() on it is allowed, but does nothing because there is nowhere to go.
Otherwise your understanding is correct.

Excel Macro Find Last Row In Column B, Select another cell A1, drag formula from A1 to last row in B

I am trying to do an AutoFill in Excel where I can make my macro:
Find the last row with data in column B (lets say this is B40), and remember.
Select A1.
Drag A1's formula to the row that was found in step 1 (which would be A40) so as to perform an autofill the formula.
I am pretty noobish at VBA so would really appreciate some help.
This should do what you need.
' Get last row with data in column B...
Dim intLastRow As Long
intLastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Fill column A down to this row...
Range("A1:A" & intLastRow).FillDown
You don't even require a Range.FillDown or Range.AutoFill method in this case.
With ActiveSheet
.Cells(1, 1).Resize(.Cells(Rows.Count, 2).End(xlUp).Row, 1) = .Cells(1, 1).Formula
End With
Just start with the top-left cell and use the Range.Resize property to expand it to the desired dimensions and transfer the formula.

Excel VBA: changing hard coded column to dynamic range to autofill to last row

Hello from an unexperienced vba user.. I'm having trouble with the code for autofill to the last row when trying to use named ranges for the column. Everything seems to work fine when I use a hard coding for the column, in this case Column CW, and what I need is to replace this column CW with a named range so that the macro will still work when adding or deleting columns in the worksheet.
I used the following named ranges:
First_Date: This is the header cell of one of the columns (in this case AP5)
Second_Row: This is the range of columns I want to copy the formulas from (AP7:CW7)
Second_Cell: The cell where I want to start to autofill (AP7)
Last_Column: This is column CW that I want to use in the code. Autofill would be up to this column and down to the last row.
After searching in different threads, I came up with the following code that seems to work fine. How can I change column CW to a named range? Or do I need to change the code?
Dim Lr As Integer
Lr = Range("First_Date").End(xlDown).Row 'Searching last row
Rows(Lr).Insert Shift:=xlDown 'Inserting new row
Range("Second_Row").AutoFill Destination:=Range(Range("Second_Cell"), Range("CW" & Lr))
Can anyone assist me here please?
This will get the job done :-)
Sub RangerFiller()
'The Cell that holds the formula B1
OriginalFormula = Cells(1, 2).Formula
'copies formula down to the last column next to B but use can use another column as
'a counting column....the column that hold the last value
Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = OriginalFormula
End Sub
Someone gave me the solution:
Change
Range("CW" & Lr)
To
Cells(Lr, Range("Last_Column").Column)
I faced a similar problem because I don't want to hard code the cell reference. I found this solution below to be useful, by using "& ______ &" to replace the cell number that can be calculated using input box or formula.
Eg.
cell1 = last row of column A
Range("CW " & cell1 &" :CW & Lr),
where cell1 = any number that can be added via input box/formula.
Hope this helps!

Copy last row in a column in Excel with VBA

I would like to copy the value of the last row in a specific Column in to another cell,
for example this is the code which I a suing to find which is the last used rown in the column G
Dim LastRow As Long
LastRow = Range("G65536").End(xlUp).Row
Right now LastRow get's the value of the address of the row, I need this code modified this way so it will copy the Value of the LastRow (not the address) and than past this value of this cell in to another Cell with address "Q1"
If anywone can Help let me know, thanks!
According to Microsoft MVP:
You should not use numbers because different versions of Excel have different number of maximum rows.
.Cells(.Rows.Count, "Q") is the very last cell at the bottom of column Q.
.Cells(.Rows.Count, "Q").End(xlUp).Row is the row number of the last non-blank cell in column Q.
So you should use
Cells(Rows.Count, "G").End(xlUp).Row
To find number of rows used:
ActiveSheet.UsedRange.Rows.Count
Range.Copy method has parameter which allows you to copy Range into required destination.
According to MSDN Range.Copy syntax looks as follows:
expression.Copy(Destination)
expression A variable that represents a Range object.
In your situation you can call it as follows:
Range("G65536").End(xlUp).Copy Range("Q1")
Where Range("Q1") should be changed to any other cell if required

Resources