Let's say we have this piece of code as an example
Worksheets"Sheets1".Cells(1,4).Formula= "=SUM(Cells(x1,y1):Cells(x2:y2))"
Is there anyway to make this work ?
The idea is that if I were to put this piece of code in a sub-routine, I could make the range of the sum dependent on the results of the subroutine.
Example, If I were to write a subroutine that counts how many cells have numbers on them (for example 20), I could then use that as range.
So something like this
count = 20
Worksheets"Sheets1".Cells(1,4).Formula= "=SUM(Cells(1,1):Cells(count:1)"
I would want this to be equivalent to writing =SUM(A1:A20)
Is it possible ?
Like this?
Worksheets("Sheets1").Cells(1,4).Formula= _
"=SUM(" & Cells(1, 1).Resize(count, 1).address & ")"
Related
I am creating a dynamic report summarizing data on multiple XLS sheets all within a single workbook. The sheets have names that tie to a specific date.
This is simplified example of what I am doing, which works fine - it gives the correct answer which is the value of the cell at reference BB38 on the sheet called "221122":
=LAMBDA(r,INDIRECT("'" & r & "'!BB38"))("221122")
Problem comes when I want to iterate this over an array of sheets using BYROW instead of just passing the sheet name to the lambda. Simple example to replicate the problem:
=BYROW({"221122"}, LAMBDA(r,INDIRECT("'" & r & "'!BB38")))
This gives a #VALUE! error, instead of the correct answer which is the reference to that same cell (as part of a one cell dynamic array result). The only way I can solve it is by adding a SUM around the INDIRECT:
=BYROW({"221122"}, LAMBDA(r,SUM(INDIRECT("'"&r&"'!BB38"))))
Apart from being ugly, what I REALLY want to do is get a group of cells (spilled) back, like this, but then I can't use the SUM trick:
=BYROW({"221120","221121","221122"}, LAMBDA(r,INDIRECT("'"&r&"'!BB38:BD38")))
So that I am aiming towards is a spilled range like this:
Column A
Column B
Column C
221120!BB38
221120!BC38
221120!BD38
221121!BB38
221121!BC38
221121!BD38
221122!BB38
221122!BC38
221122!BD38
I know that you can't pass a dynamic function to INDIRECT but that's not what I am doing here - I am passing a single row of the dynamic array, represented by r.
In comment, Harun24hr points out correctly that BYROW can't return a dynamic array - that's why SUM worked. My own 'hack' way around this was to get the individual 1xN ranges of cells representing BB38, BC38 and BD38 and then HSTACK them together, e.g.:
a, BYROW(sheets, LAMBDA(r, SUM(INDIRECT("'" & r & "'!AY38")))),
b, BYROW(sheets, LAMBDA(r, SUM(INDIRECT("'" & r & "'!AZ38")))),
c, BYROW(sheets, LAMBDA(r, SUM(INDIRECT("'" & r & "'!BA38")))),
d, BYROW(sheets, LAMBDA(r, SUM(INDIRECT("'" & r & "'!BB38")))),
HSTACK(a,b,c,d)
Real question is: is there a more elegant / scalable way than HSTACK 1xN columns together?
Any ideas please? Thank you.
How about this approach:
=LET(start,221120,
end,221122,
DROP(REDUCE(0,SEQUENCE(1+end-start,,start),LAMBDA(s,e,VSTACK(s,INDIRECT("'"&e&"'!BB38:BD38)))),1))
Or simple =VSTACK('221120:221122'!BB38:BD38) based on this answer by JvdV: https://stackoverflow.com/a/74077560/12634230
I am trying to enter a "=$M15-$N15" formula to cell A15. However, need to use variable (r) instead of 15 as I don't know that value until later in the code. Trying the below but it is not working. What am I doing wrong?
Range("A" & r).formula = "="$M" & r - "$N" & r"
after the code is run, I need to have a formula ($M15-$N15) in Cell A 15.
I suspect you're looking for the INDIRECT function. An example of this, based on your explanation, would look like:
=INDIRECT("M"&A1)-INDIRECT("N"&A1)
If you're actually looking for VBA, you could use the following:
Range("A" & r).formula = "=$M" & r & "-$N" & r
However, this makes me suspect this is part of some over-arching routine, and, without knowing more, I can only speculate there might be a better or more efficient method(s) for going about it.
I'm doing some data crunching work in Excel. What I need to do is to fetch values from a Cell (Say D3) from sequentially numbered workbooks like 1.xlsx, 2.xlsx, 3.xlsx...so on.
I can't use INDIRECT() function as it would require all the target files to be opened (which is not possible in my case). So, I'm using the INDEX() function with the following values:
INDEX( [1.xlsx]Sheet1!D:D, 1,1 )
Now, I want the bold part of the formula to be dyamic based on the number on left column as shown in the image below:
Somebody please suggest solution to the problem at hand as I'm tired on googling :)
Thanks in advance.
Perhaps someone will prove me wrong, but I don't believe it is possible to generate such dynamic referencing without resource to INDIRECT, unless you are happy with a longhand (and static) method such as:
=INDEX(CHOOSE(ROWS($1:1),[1.xlsx]Sheet1!D:D,[2.xlsx]Sheet1!D:D,[3.xlsx]Sheet1!D:D),1,1)
though this requires that each workbook reference be entered explicitly within the formula. As such, this also means that these cannot be created via reference to any cells within the worksheet, as you might have hoped.
Or, if VBA is an option:
Sub Indexing()
Dim i As Long
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
Range("B" & i).Formula = "=INDEX([" & Range("A" & i).Value & ".xlsx]Sheet1!D:D,1,1)"
Next i
End Sub
Regards
I need to write a macro.
I've got a workbook with ~ 30000 rows (changes daily).
I need to search for expression "TRADE" within the strings in cells from column (A)
If string inside the cell contain expression TRADE I need to change string in relevant cell in column (B) (the same row) to expression "TRADEIN"
If condition is not met relevant cells from column (B) need to stay unchanged
What have I learned so far:
Formula =IF(ISNUMBER(FIND("TRADE", A1 )), 1, 2) changes adjacent cell value accordingly ONLY if placed directly inside cell and copied down in Excel.
Problems starts when I try to have string as an outcome
Formula: =IF(ISNUMBER(FIND("TRADE", A1 )), "TRADEIN", "") won't work ->error
Formula: =IF(ISNUMBER(FIND("TRADE", A1 )), ""TRADEIN"", "") won't work ->error
Then any attempts to make my macro insert more complex formulas into cells from VBA failed i.e.:
Below works fine:
For i=1 to i=NumberOfRows
ActiveSheet.Cells(i, 2).Formula = "= 2+2"
next i
Below won't work (again, formula works if placed in the cell directly):
For i=1 to i=NumberOfRows
ActiveSheet.Cells(i, 2).Formula = "=IF(ISNUMBER(FIND("TRADE", (i, 1)), 1, 2)"
next i
I think there's no point in listing all my failed attempts to make it work so far (loads of useless lines to read I presume) but by all means - correct me if I'm wrong.
I can't find solution as specific as my task and have got problems altering some found online whilst other won't work for me at all. Perhaps don't exactly know how to ask for what I need in the most effective way. Be very basic and try not to miss out any declarations from proposed modules/subs if you can - I'm not yet confident when it comes to using and creating objects and methods outside of a few examples I followed, or choosing/using the right type of variables with compatible methods/functions etc.
Using VBA this is how would accomplish the goal. This will find the last row used in column A to set the range to work through.
Sub test()
Dim w As Range
lrow = Range("A1", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Count
For Each w In Range("A1:A" & lrow).Cells
If w.Value = "trade" Then
w.Offset(0, 1).Value = "tradein"
End If
Next w
End Sub
Practice using the auto filter, once you have that worked out use the macro recorder to get a code to work on.
Select column A and the goto Data=>Filter=>text Filter=>Contains....type the word in the box to filter for.
i have very recently started to learn to create VBA macros in Excel, however, I am completely stumped as to how to create a CSE array formula within my macro
I would like to define a variable as the row in which the value in column A in a sheet = value 1 and column B = value 2
within excel itself, the formula below works perfectly:
{=Match(1,(A:A=1)*(B:B=2),0)}
However, I would now like to use it within my macro along the lines of:
variable = {=Match(1,(Sheets("Right Sheet").range("A:A")=value1)*(Sheets("Right Sheet").range("B:B")=value2),0)}
Anyone able to provide any light on this?
I have played with all sorts of variants of ArrayFormula and had no success at all!
Ideally I would like my value1 and value2 numbers to be equal to variables (1 and 2) defined previously in my macro
Thanks in advance
This (eg) worked for me:
Debug.Print Application.Evaluate("SUM(A1:A5*B1:B5)")
you need to build up the formula into a string, and leave off the "=" and "{}".
Debug.Print Application.Evaluate("Match(1,(Sheet1!A:A=" & val1 & _
")*(Sheet1!B:B=" & val2 & "),0)")