the problem I am trying to figure out is a bit complicated and detailed but I will try to simplify it with an example.
I am looking to use vba to change the array formula with a conditional statement. An example of what the code would look like is below
If (InStr(1, Range("E" & i).Value, "X")) > 0 Then
With outputSheet
Range("I" & i).FormulaArray = Sumproduct(('Sheet1'!$A$57:$A$104=$B2)*('Sheet1'!$E$56:$AZ$56=$H2),'Sheet1'!$E$57:$AZ$104)
End With
Else [Same code as above for Array formula]
End If
Initially I was using the following code to copy the Array formula down however using that I cannot use a conditional statement where the formula is dynamic according to cells.
Range("G2").AutoFill .Range("G2:G" & OutputLastRow)
If however, I can somehow make the $B2 into B&i then that would do the trick for me.
On just a highlevel, the idea was that the output cell would look at some conditions in the same row and different columns and change array formula according to the conditions. It would look at it for each cell and move down until it hits the last row
Any suggestions on how this can be tackled. I hope the problem is clear. Thanks so much!!
Related
I'm still getting to grips with VBA and have an issue with something I'm trying to do and was hoping i can get some help. (sorry if it doesn't make much sense, I don't have the lingo).
This part of the code is where I need it to put a value in the last cell on col A from col I (row dependant of my variable).
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Value = Range("I" & RowCount)
If i replace the variable "RowCount" with "1" it gives me what I want. I have used the same wording for "Range("I" & RowCount)" multiple times with in the macro but for some reason doesn't like it in this...
Any help?
So what I would like to do is add a sum formula to a cell, so it can be edited later(normally, not through vba). It sums up some cells, but the amount of cells is not always the same. Sometimes it's 4 cells, sometimes it's 10. So I'm trying to have:
lastrow = Sheets(7).Cells(Sheets(7).Rows.Count, "A").End(xlUp).Row
then using it:
Sheets(7).Range("B" & lastrow + 1).Formula = "=SUM(b2:b&"lastrow")"
My problem is the syntax actually, I can't seem to make it right. How to add lastrow to this formula?
Hope it's understandable, English is not my native language.
You're missing to concatenate.
Sheets(7).Range("B" & lastrow + 1).Formula = "=SUM(b2:b" & lastrow & ")"
Instead of using the .End(xlUp) method, I'd suggest that using Named Ranges and in particular the built-in structured referencing of Excel Tables/ListObjects would make for more robust and simpler code.
Using Named Ranges avoids hard-coding references in your code. If you hard code cell address into your code, those references will be pointing at the wrong place if you (or a user) later adds new rows/columns above/to the left of those hard-coded references. Using Names avoids this, and makes for more robust code.
I almost always use Excel Tables aka ListObjects to hold any data that VBA interacts with for the same reason...ListObjects are dynamic named ranges that Excel automatically expands/contracts to suit the data, and unlike .End(xlUp) they won't throw your code off if the column contains blanks.
So I'd do it like this:
Range("SomeNamedRange") = "=SUM(" & Range("SomeTable[SomeColumn]").Address & ")"
...or more likely, I'd use the [] shorthand notation:
[SomeNamedRange] = "=SUM(" & [SomeTable[SomeColumn]].Address & ")"
First time poster here, I've been searching for the last hour without success so I'm turning to asking for help... My limited VBA knowledge might be a factor here.
I'm creating a quick macro that will sum values cumulatively. I regularly use =SUM($A$2:A2) and AutoFill down to get a cumulative sum. While I can be very quick at doing this, I have to do it several times per day in various sheets. My goal was to have the absolute $A$2 reference to be variable based on the current selected cell.
So let's assume I am trying to add this formula to cell B2. I know I can do:
ActiveCell.FormulaR1C1 = "=SUM(R2C1:RC[-1])"
However, this formula is unusable if my starting cell is anything but B2 and I am trying to cumulatively sum data in various columns.
Is it possible to have VBA count the number of columns between the selected cell and A2 and use this as a variable to set the absolute reference? Such as:
ActiveCell.FormulaR1C1 = "=SUM(R2Cvar:RC[-1])"
where var is =COUNTA(R2C1:ActiveCell)+COUNTBLANK(R2C1:ActiveCell)-1
I know the above code isn't valid, but it's the only way I can think of explaining what I'm trying to achieve.
You'll have to concatenate your formula eg
ActiveCell.FormulaR1C1 = "=SUM(R2C" & var & ":RC[-1])"
But to get your variable you can do
var = (ActiveCell.Column - 1)
Something like:
Sub ytrewq()
Dim s As String
With ActiveCell
s = .Offset(0, -1).Address(0, 0)
.Formula = "=SUM($A$2:" & s & ")"
End With
End Sub
The Offset() generates the address of the cell "just to the left".
I'm trying to assign a cell a formula using VBA, but every time I run the code it assign to the cell the result not the formula itself, like when I enter the formula using the excel spreadsheet.
Does anyone know how to display the formula using a macro within a cell and not the formula result.
I'm asking this because I need to insert this line within an existing sheet among other data and then run another macro to keep it updated, and the macro depends on this formula.
The code I'm using
Cells(C, 9).Formula = Application.Index(Plan2.Range("B2:D10000"), _
Application.Match(Plan1.Range("B" & C) & Range("F" & C), Plan2.Range("A2:A10000"), 0), 3)
As you can see this formula depends on the row that its inserted.
Your Formula is ultimately being reduced to whatever is returned by Application.Index. Unless the value being returned there is an actual formula string then you will just get a number as the result and this is set to the .Formula.
If you want to actually set the formula, you need to create a string in VBA that represents the formula to use. In this case, that string would look something like:
Cells(C, 9).Formula = "=INDEX(Plan2!B2:D10000, MATCH(Plan1!B" & C & "..."
where you concatenate in the dynamic parts. The end result needs to look like a normal formula. The Application.XXX and Application.WorksheetFunction.XXX functions return actual results, not pieces that can be combined to create a formula.
Is it possible to write formulas across a range in Excel from VB.Net? I'm using a String array to hold a list of formulas that I would like to apply to an Excel range, instead of looping through and writing them one at a time.
This line is what I am attempting to use to write to a range:
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).Formula = formulas
The formula is being written to Excel, but Excel is actually displaying the formula, instead of the calculated value. If I write each formula to each cell like this:
xlWorkSheet.Range("AF" & intCurrentRow.ToString).Formula = formulas(0)
xlWorkSheet.Range("AG" & intCurrentRow.ToString).Formula = formulas(1)
It works perfectly fine and Excel displays the calculated values as it should. Almost seems like I'm missing a step but I haven't been able to find anything in my research.
As soon as I hit post I figured out the problem. Instead of using the .Formula property of an Excel.Range, you have to use the .FormulaArray property instead.
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).FormulaArray = formulas