Range object with variables - vba

I've found a lot of examples using the Range identifier and a variable as the end of the range but not as the first part.
Range("B2:B" & lastrow)
works for me, as long as I identify what the lastrow variable is.
But! If I want to do something where the start of my range is the last row and the end of my range is five rows below that, how would I write that?
Range("B" & lastrow":B" & lastrow + 5)
doesn't seem to work, and I've shifted around the quotation marks into every position I could think of.
Any and all help is greatly appreciated!

You have a typo. You are missing an ampersand after lastrow. It should be:
Range("B" & lastrow & ":B" & lastrow + 5)

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

Writing a formula as Syntax in VBA

I want to run a rank formula across a range of cells. I am scratching my head as to why it's not working. Lastrow is just the formula that counts the number of rows.
Range("B1:B" & Lastrow).Formula = "=RANK(A1,Offset(" & Chr$(36) & "A1" & Chr$(36) & "," & Lastrow & ",0))"
I feel like it's something wrong with Chr$(36), but when I try Chr(36) it doesn't work either. (removing these chr(36)'s and just having Offset(A1... etc) works fine).
Thanks in Advance!
It's hard to see what you want to do. The line of VBA code below will, at least, work.
Range("B1:B" & LastRow).Formula = "=RANK(ROW(),$A1:$A$" & LastRow & ",0)"
Your issue seems to be with the RANK() function. It has 3 arguments, (1) the rank, (2) the range in which to find the rank and (3) Ascending/Descending. In your formula the first argument is missing and your "Offset(" & Chr$(36) & "A1" & Chr$(36) & "," & LastRow" doesn't describe a range, read as Offset($A1$,300 with closing bracket missing.
My above formula suggests the Row number as rank, meaning 1 for Row 1, 2 for Row 2 etc. but descending, as indicated by the 3rd argument (taken from your formula) and, as second argument a range in column A between A1 and the LastRow. It probably isn't what you wanted but I hope you will be able to tweak it.

Vlookup for dates in other sheets

I have a problem! Even though my Vlookup for dates seems to be fine, when I run the macro, instead of showing dates, it shows some random numbers! Below is the code for vlookup! I did not include the rest of the code because it has nothing to do with this part! The vlookup is correct as of where it refers to and staff beacuse in the sheets it runs perfectly as a formula.. Only inside a macro is not working!! Should I add anything to the code below to indicate that it is a date so as for the macro to behave differently? please help me :)
Sheets("Tracker1").Select
lastrow = Range("B" & Rows.Count).End(xlUp).Row
Range("G2:G" & lastrow).Formula = "=VLOOKUP(B2,'Programare'!C:F,4,FALSE))"
You can set explicitly the format to the dat format you want. i.e.
With Worksheets("Tracker1")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
With .Range("G2:G" & lastrow)
.Formula = "=VLOOKUP(B2,'Programare'!C:F,4,FALSE))"
.NumberFormat = "d/m/yy" ' <---------------------- Set the date format explicitly
End With
End With

Finding last row when there are more than 65536 rows

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.

Using SUMIF until last row in vba

I'm trying to incorporate a SUMIF formula into my macro. The code in excel looks like this: =SUMIF('WSO Interest' H2:H46, '20140618 Loans' D10, 'WSO Interest' S2:S46)
I set my dim as i and lastrow as integer
I already set that so that I can find the last row of the column and continue my loop until the last row.
I also used
Sheets("20140618 Loans").Select
Range("A10").Select
Selection.End(xlDown).Select
lastrow = ActiveCell.Row
to find the last row filled with text.
This is what
I have so far:
Range("W10").select
For i = 10 to lastrow
SUMIF formula would go here
Next i
So basically what I'm trying to do is use the SUMIF formula in my macro to start at W10 and keep calculating the SUMIF formula until it reaches the last row. Thanks for the help and feel free to ask any questions.
You can use the Formula property of a Range object to set the formula.
Range("W" & i).Formula = "=SUMIF('WSO Interest'!H2:H" & lastrow & _
", '20140618 Loans'!D10, 'WSO Interest'!S2:S" & lastrow & ")"
The & operator is used to concatenate strings together.
Dim r As Range
Set r = Range("W10", Range("W10").End(xlDown)) --Change it per requirement .....
For Each cell In r
Debug.Print cell.Address --Your Code goes here I guess
Next