CountIf referencing named range and starting on row 3 - vba

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) & ")"

Related

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.

VBA copy range of rows

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

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)

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