Finding last row when there are more than 65536 rows - vba

ws1.Cells(1, i).Copy ws2.Range("a65536").End(xlUp).Offset(1, 0)
I am looping through the sheet so i is a part of loop. The number of rows are more than 65536 , then how can I adjust this. Thanks in advance.

You should use a count of rows rather than a hard coded value.
With ws2
ws1.Cells(1, i).Copy .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
End With

Simplest approach is wks.usedrange.rows.count. Reasonably sure the bugs related to usedrange updating when saving the workbook were fixed in post-excel...

All concise solutions have cases where they fail. The accepted answer will produce the wrong result in the circumstance where the column is completely blank.
Here is a different way that works as long as there are no blanks above or in-between the data:
ws1.Cells(, i).Copy ws2.[INDEX(A:A,COUNTA(A:A)+1)]

Remember that you can always get the last used range on a excel worksheet as follow:
Dim lastRange as Range
Dim lastColumn as Long
Dim lastRow as Long
Set lastRange = Worksheets(?).UsedRange.SpecialCells(xlCellTypeLastCell)
lastColumn = lastRange.Column 'return the last column index
lastRow = lastRange.Row 'return the last row index
So to answer your question, you can use my previous example like this:
ws1.Cells(1, i).Copy ws2.Range("A" & lastRow).Offset(1, 0) 'or
ws1.Cells(1, i).Copy ws2.Cells(lastRow, 1).Offset(1, 0)
Both statements will do the job for you. Just be conscious that you have to redefine the lastRange Object and the lastRow variable within the loop, otherwise the lastRange variable will hold the value of its initialization.
Good Luck!
Andrés Alejandro García Hurtado.

Related

VBA: Considering only the value of the last cell in A and extend selection untill W

I have several columns from A to W. some aer full and some empty and some with "0", but i need to get the last value in A and selecting from A17 untill W (LastValueOfA)
For example A is starting from rows 17, B arrive untill 30 and W is full of 0 till the end of the sheet. I want a selection from A17 till W that stops where there is the last value of A and then copy or cut the values and append them on another sheet of the same file called "order inputs".
At the moment i only found the way to found and select the lastrow with values inside, but it consider all the columns obviously, not only A:
Dim wb As Workbook
LASTROW = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
Range("a17:W" & LASTROW).Select
Thanks in advance.
Since your last comments on #K.Davis answer, I guess your column A values are resulting out of a formula the value of which you consider to be valid when starting with "US"
So you may try this:
With ThisWorkbook.Worksheets("Sheet1") ' change "Sheet1" to your actual sheet name
.Range("A17:W" & .Columns("A").Find("US*", SearchDirection:=xlPrevious, LookIn:=xlValues).Row).Select
End With
I believe the issue is related to formulas that output an empty string. While the cell may appear to be blank, it's technically not. You need to search in the cell's Values.
You can use Find() to get around this.
Try this:
Option Explicit
Sub Test()
Dim ws As Worksheet, lastRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lastRow = ws.Columns("A").Find("*", SearchDirection:=xlPrevious, LookIn:=xlValues).Row
MsgBox lastRow
End Sub
"*" returns any value - it's just a wild card.
xlPrevious will search from the bottom-up
and finally, xlValues ensures you are looking at the Cell's output - not at the cell.

Sum a range from another worksheet

I'm trying to get this cell to total a range from another worksheet but I keep getting stuck. The range rows and columns vary but the starting point is always C2. I need to total from C2 to the rest of the used range (aka exclude columns A and B as well as row 1 but include everything else).
Please help if you can.
Range("A1").Formula = "Wage Totals"
Range("A2").Formula = "=SUM(" & ActiveWorkbook.Sheets("Wage").Range(Cells(2, 3), Cells.SpecialCells(xlCellTypeLastCell)).Address(False, False) & ")"
Check out this link Using SUM() in VBA
You can use that idea to change your code to:
Dim lastCol As Long
Dim lastRow As Long
lastCol = Sheets("Wage").Cells(1, Sheets("Wage").Columns.Count).End(xlToLeft).Column
lastRow = Sheets("Wage").Cells(Sheets("Wage").Rows.Count, 1).End(xlUp).Row
Range("A1").Value = "Wage Totals"
Range("A2").Value = WorksheetFunction.Sum(Sheets("Wage").Range(Sheets("Wage").Cells(2, 3), Sheets("Wage").Cells(lastRow, lastCol)))
This is pretty naive solution tho becuase it assumes the usedrange limits can be found by looking for last used row on Col "A" and last used col on Row 1. You can change that method of finding the limits.

Create Range excluding column in middle

I have the problem of needing to exclude a column in the middle of my excel worksheet. Is there a way of creating a range that excludes a column not on the edges of the data. The range is dynamic ranging from A1:AA#. The column "F" needs to be excluded from this range. This range needs to be stored for use in a pivotTable.
Range("A1:E4,G1:J4").Select
This is how the excel macro recorder creates a range with a gap, but I cannot find a way to modify this so the last cell is dynamic.
Thanks.
Just as you should avoid using the .Select Method, you should also avoid using the .UsedRange when possible (See here).
You can try something like this. It is not as clean, but may prove to be less bug prone.
Dim LRow As Long
LRow = Range("A" & Rows.Count).End(xlUp).Row
Dim MyRange1 As Range, MyRange2 As Range, BigRange As Range
Set MyRange1 = Range("A1:E" & LRow)
Set MyRange2 = Range("G1:J" & LRow)
Set BigRange = Application.Union(MyRange1, MyRange2)
BigRange.Select
You can then refer to your BigRange directly moving forward.
If you have only one set of data in your sheet, you could try something like that :
Intersect(ActiveSheet.UsedRange, Range("A:E,G:AA")).Select
This will select everything that contains data up to column AA on the sheet except for column F.
Whenever possible , you should avoid using .select .activate but you only provide one line of your code so I can't help you much on that part except redirect you to this.
you could use
Dim rng As Range
Set rng = Intersect(Range("A:E,G:AA"), Rows(1).Resize(Cells(Rows.Count, 1).End(xlUp).Row))
where the column index in Cells(Rows.Count, 1) lets you choose what column size your range after

excel: usedrange with columns of table

Is there any way to use UsedRange to select from first to last column from a table?
Here is the original code:
Worksheets("Sheet1").UsedRange.Columns("E").Cells
But it gets from first row to last maximum of Excel and I don't want this. So I tried to do something like this:
Dim LastRow As Integer
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
With ws.UsedRange.Columns("E2:E & LastRow").Cells
Although this doesn't seem to work here, so I was in doubt if there is a correct way to do this or the UsedRange it's not the best for that.
In addition, after this I want to use THIS code provided by Jeeped, but I need to know if it's possible to solve this problem first.
You could use the range directly,
with ws
with .range(.cells(1, "E"), .cells(.rows.count, "E").end(xlup))
'do something with the cells in column E
end with
end with
You could use .UsedRange with Intersect
with Intersect(ws.UsedRange, ws.columns("E"))
'do something with the cells in column E
end with
Try this, but never use .Select.
With ws
.Range("E2:E" & LastRow).Select
End with

Filter a column and select the last used row

I want to filter a particular column in Excel sheet and then select the range of it until the last used row.
For getting a particular column I am using
ActiveSheet.Range("$A$1:$D30").AutoFilter Field:=3 , Criteria1:= "1"
And for finding the last row I am using
Cells(ActiveSheet.Rows.Count,1).End(xlUp).Row
I am not able to combine both together. If I run both the commands together, I am getting the entire results instead of filtered results.
I know it may be a simple one, But I am not able to do it. Can anybody help me in doing it?
My requirement is that the first the column should be filtered and then the range of until the last unused row should be selected. So that I can do some commands using the selection.
Try this:
With Range("A1:D" & Range("a1048576").End(xlUp).Row)
.AutoFilter Field:=3, Criteria1:="1"
.Resize(, 1).Offset(1).SpecialCells(xlCellTypeVisible).Select
End With
Use the SpecialCells Method with the xlCellTypeVisible argument. I also qualified the worksheet to work with, since it's way more stable than using ActiveSheet (should always be avoided, unless absolutely necessary.)
Dim ws as Worksheet
Set ws = Sheets("mySheet") 'change to the sheet name you need
Dim lRow as Long
lRow = ws.Cells(ws.Rows.Count,1).End(xlUp).Row
ws.Range("$A$1:$D" & lRow).AutoFilter Field:=3 , Criteria1:= "1"
Dim rRng as Range, cel as Range
Set rRng = ws.Range("A2:A" & lRow).SpecialCells(xlCellTypeVisible) 'assumes header row in column 1
'Updated code based on your comments.
For each cel in rRng
objRecipients.Add cel
Next
ws.AutoFilterMode = False