Set sum range when range varies - vba

I need to use a sum formula in my VBA script that always starts at cell B10 but, will finish at a different cell (always in column B) depending on results of the previous macro's.
The first cell for sum range will always be cell B10 ... the end of the sum range will always be 3 rows above the cell that formula is populating.
I tried the following but, I get
'Compile error: Argument not optional' error.
ActiveCell.Formula = "=SUM(B10:" & ActiveCell.Offset(-3, 0).Range & "))"
How can I use a varying cell reference in my formula?

As you are only looking for the Row value you can better use that property directly:
ActiveCell.Formula = "=SUM(B10:B" & (ActiveCell.Row - 3) & ")"

Related

Update advanced filter for a range which has new rows added

I have a worksheet containing a list of criteria in cells A1 to J2. Row 1 is the header.
Below i have a a table with all my data items. Cells A8 to J with a dynamic numbre of rows
Sheets("D0022").Range("A8:" & Sheets("D0022").Cells(Rows.Count, "J").End(xlUp).Row).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("D0022").Range("a1:j2"), Unique:=True
When i run this i get an
Runtime 1004
Application defined or object defined error
Can i have assistance on why my VBA code does not work
Try changing of Sheets("D0022").Range("A8:" & Sheets("D0022").Cells(Rows.Count, "J").End(...
with
Sheets("D0022").Range("A8:J" & Sheets("D0022").Cells(Rows.Count, "J").End(...
"A8:J" instead of "A8:".
Otherwise you do not set the column of the range.
Sheets("D0022").Cells(Rows.Count, "J").End(xlUp).Row calculates only the last row of column J:J...

putting a string from a cell into the middle of my index-match VBa script

I am trying to use the index-match formula to reorganize data such that all of the names in column J that have a matching value in column A will be placed in the same spot. I'm going to do this for 5 different columns so that the 5 names on a team will be in the same row as the name of the corresponding client.
My issue is that the index-match formula needs to be able to dynamically shorten or lengthen the size the arrays it uses based on how many clients there are when the VBA script is run.
I can dynamically determine what numbers I need in the formula using COUNTA, but the code will not compile when I try to put it in my formula. My formula is below
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & ",MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0)"
As you can see I need the strings in cells J3 and J1 to be used as the arrays for the index match. J3 = $J$2:$J$2369 and J1 = $A$2:$A$1113
When I run the code it gives me a "Application-Defined or Object-defined error."
You need to use the Range member of worksheet
so use 'test sheet two'!Range("J2:J2369") rather than 'test sheet two'!("J2:J2369").
The following runs
ActiveCell.Formula = _
"=INDEX('test sheet two'!Range(""" & Range("J3") & """) _
,MATCH(Sheet1!A5,'test sheet two'!Range(""" & Range("J1") & """),0))"
Your formula was not including the column criteria for the INDEX Function.
Try:
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & "," & _
"MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0), 1)"
Notice the additional , 1)" on the end of the formula.
Also, you do not have to first Select the cell which you want to enter the formula in, you could just use:
Range("B7").Formula =

Remove single quote VBA function

To simplify, I have two sheets in an Excel workbook.
If I write =sheet2!R3C2 in one of the cells of sheet1, the cell takes the value of the cell R3C2 of sheet2 correctly.
I would like to generalize that for many rows in VBA, so my code is:
row = XX a long
temp = "=sheet2!R" & row - 1 & "C2"
Range("c" & row).Value = temp
But when I use this code, the value of the cell(row,3) is =sheet2!'RXXC2'
How can I remove the single quotes ??
Range("C" & row).Formula = temp
would produce the correct formula in your cell.
What you should consider doing instead of looping is
Range("A1").Formula = "=Sheet2!$B1"
Range("A1").Resize(100, 1).Formula = Range("A1").Formula
The first line inserts a formula =Sheet2!$B1 in cell A1 of your active sheet. The $ dollar sign assures that the column will not be incremented (same applies with numbers)
Then the second line duplicates the formula across 100 rows down from A1 replacing the number after the B column
So now
A1 =Sheet2!B1
A2 =Sheet2!B2
A3 =Sheet2!B3
...
Also, it's a bit unclear what you're trying to actually do so consider another option which is saving the value of formula into another range using the Evaluate() function
Range("c" & row).Value = Evaluate(temp) Or Range("C" & Row).Value = [temp]
try to write Range("c" & row).FormulaR1C1=temp
You want to set the formula, not the value:
Range("c"&row).FormulaR1C1 = temp

How to find the last value with specific conditions?

Sheet 1 column A has the following values (it has around 3000 records. I’ve given the below sample values). I need to find the last value of a specific text.
RVT-01
RVT-02
RVT-03
RVT-04
RVT-05
RVT-06
RHT-01
RHT-02
RHT-03
RHT-04
RHT-05
ROI-01
ROI-02
ROI-03
SWO-01
SWO-02
SWO-03
SOR-01
SOR-02
SOR-03
SOR-04
SOR-05
SOR-06
SOR-07
Using VBA code
If enter short tex in sheet1.cells(2,2) = SWO , I need the last value in sheet1.cells(2,4)=SWO-03
If I enter sheet1.cells(2,2) = RHT , I need the last value in sheet1.cells(2,4)=RHT-05
If I enter sheet1.cells(2,2) = RVT , I need the last value in sheet1.cells(2,4)=RVT-06
If I enter sheet1.cells(2,2) = SOR , I need the last value in sheet1.cells(2,4)=SOR-07
What would be the VBA code for the above process?
As Skip Intro suggested, there is no need for VBA: in Column B, put a formula like this:
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,RIGHT(TRIM(A:A),2),"") (to get the just the max number):
or
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,A:A,"") (to get the complete contents of the cell)
Both will show you the highest values. Then you could AutoFilter that column, hiding the blanks and voila :)
Or
=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"")
will enable you to use SpecialCells in VBA to get a range that you can interrogate for the maximum values in each group, as below:
Sub test()
Dim rng As Range
Dim cell
Range("B1:B" & Range("A65536").End(xlUp).Row).Formula = "=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"""")"
Set rng = Range(Range("B1:B" & Range("A65536").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).Offset(0, -1).Address)
For Each cell In rng
Debug.Print cell.Address & " =" & cell.Value
MsgBox cell.Address & " =" & cell.Value
Next
End Sub
For more information on the SpecialCells magic tricks, see How to delete multiple rows without a loop in Excel VBA.

Excel VBA - Sum function

I'm trying to calculate the sum of my columns (column I). From 1 to the last record in I it has. When I record a macro I get this as output, but this is gibberish to me.
ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-4]C)"
I found another topic and there they said
LastRow = .Range("I" & .rows.Count).End(xlUp).row
Range("I"&LastRow) = "SUM(I1:I...)"
Except in my case, I can't figure how to enter the lastrow of I in it.
All help is welcome :)
There are two ways of referencing a cell - 'R1C1' and 'A1'. The former works like co-ordinates, with a relative number of rows (R) and cells (C).
The other reference style refers to the cell name on the sheet - B6, F67 etc.
Let's say you want to put your Sum() in cell B1 and LastRow has a value of 6:
ActiveSheet.Range("B1") = "=Sum(I1:I" & LastRow & ")"
Would insert the following function in cell B1:
=SUM(I1:I6)