Vlookup for dates in other sheets - vba

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

Related

VBA FormulaR1C1 property across new sheet and multiple columns

I'm afraid I can't wrap my head around the FormulaR1C1 property when trying to find multiple columns in another sheet, I have these formulas currently (very slowly) autofilling:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(R2,LU!C[1]:C[5]),"""")"
Range("F2:F" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,2,true),"""")"
Range("G2:G" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,3,true),"""")"
Range("H2:H" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,4,true),"""")"
Range("I2:I" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,5,true),"""")"
Range("J2:J" & LastRow).Formula = _
"=COUNTIF(A:A,'Pivot Counter'!A7)"
You can see in the top formula, I've attempted it to no success. I keep getting an application defined error, but don't know enough about the property to fix it.
The formula
Range("F2:F" & LastRow).Formula ="=IFERROR(VLOOKUP(A2,LU!A:E,2,true),"""")"
converted to R1C1 reference style is
Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-5],LU!C[-5]:C[-1],2,True),"""")"
To understand it completely considering the following:
the cell location where you enter the formula is the starting point
starting column is F
column A is 5 columns to the left of F, so A2 converts to RC[-5] (same row, 5 columns to the left of starting point (or think of 6th column (F) and move -5 from there))
same principle for the range lookup. You want to look in columns A:E in sheet LU. So -5 columns from column F (remember starting point is column F, even though it's now looking at a different sheet) to -1 column
Knowing this, you can convert the other formulas, even the COUNTIF.
Also, if you get stuck again, following #BruceWayne's suggestion of turning R1C1 reference style under Excel > Options > Formulas to see what the formula would be when you type in manually.

Range object with variables

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)

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

Calculating formulas in excel cells with VBA?

I have a series of Excel files all in the same format. The amount of data in each excel file differs. I am making a code that will loop through a folder and perform calculations with all excel files.
I am using the following code in VBA to determine the number of rows in an excel file:
i = .Cells(.Rows.Count, 2).End(xlUp).Row
I am using the following formula to perform calculations on a range of cells:
With .Range("D2:D" & i)
.Formula = "=Log(B2)"
End With
How would I calculate the sum of all values in the "D" column in the next available cell in D? This is how the formula would look in theory
j = i + 1
With .Range("D" & j)
.Formula = "=Sum(D2:D & i)"
End With
How would I use the D cell with the sum for future calculations? Lets say I wanted E1 "=(D2-$D$ & j)^2"
Sorry for the vague title, I didn't know how to describe this problem.
As follow up from comments, this one works:
With .Range("D" & j)
.Formula = "=SUM(D2:D" & i & ")"
End With

#NAME error when setting cell formula through VBA.

I am writing a macro that requires me to get the average of the values in a column with an unknown number of rows. I use this to get the number of the last row:
Dim lastRow As Long
lastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
That works. What doesn't work is when I try to use it here:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE('table1'!AM2:AM" & lastRow & ")"
Doing that, I get a result of #NAME?. How can I fix this?
Change ActiveCell.FormulaR1C1 to ActiveCell.Formula since you're using basic A1 notation and not R1C1.