Is it possible to enter a formula in VBA that refers to an address defined by a named range?
If my range is called "Table" I would reference a cell in the following way (with counters "n_row" and "n_col")
Range("Table")(n_row, n_col)
How would I do this in a Cell formula?
The best I could come up with is this, which clearly didn't work.
Range("Test").Formula = "=10 - Table(n_row, n_col)"
Use the Index Formula
Range("Test").Formula = "=10 -INDEX(Table," & n_row & "," & n_col & ")"
Related
I need to extract the current cell in VBA without passing it in parameter of the function.
I mean, for example, if the formula is in cell B35, I want to extract the address (line = 35, column = 2), in the VBA code of the formula.
I thought the active cell would do it, but no, it extracts the address of the cell where the cursor is.
Do you know how I could do it?
Thanks in advance for help.
I think you mean Application.Caller which is the object that calls VBA. It can be used inside a function to return the cell calling the UDF, not the ActiveCell
Application.Caller property (Excel)
An easy example would be this:
Public Function ThisCell() As String
ThisCell = "ROW:" & Application.Caller.Row & " - COLUMN:" & Application.Caller.Column
End Function
If you type now in any cell =ThisCell() it will return its row and column:
Notice the output is different in all of them, even if they use the same UDF with no arguments.
A similar one to Foxfire And Burns And Burns' answer, but no vba needed.
Apply this formula, then copy it and paste as values.
="line = "&ROW()& " column= "&COLUMN()
Try below codes
Sub ExAddress()
MsgBox "Row: " & ActiveCell.Row & vbNewLine & _
"Column: " & ActiveCell.Column
End Sub
I wrote code to put SUMIFs in a cell based on sheet name. newFileName and oldFileName are declared variables used as sheet names.
Sheets(newFileName).Select
Range("C2").Formula = WorksheetFunction.SumIfs(Sheets(oldFileName).Range("$G:$G"), Sheets(oldFileName).Range("F:F"), Range("B2"))
This works!
However, I want to put the SUMIFS formula in cell C2
Sheets(newFileName).Select
Range("C2").Formula = "=SumIfs(Sheets(oldFileName).Range("$G:G"), Sheets(oldFileName).Range("F:F"), Range("B2"))"
This gives a compile error. Any ideas how to make it work?
You need to write the formula in formula syntax, which means ' before and after the sheet name and ! before the ranges. More importantly, a formula is just a String so you should not be using Sheets or Range calls when constructing it:
Sheets(newFileName).Range("C2").Formula = "=SumIfs('" & oldFileName & "'!G:G,'" & oldFileName & "'!F:F,B2)"
I have a formula that I'm trying to insert in cell D8. The value is in cell M15, so I'm trying to take that value and make the formula (value in M15 - D12). I have the following code, but I keep getting an error (400 error). Can someone help figure this out?
Dim chargeinput As Worksheet
Set chargeinput = model.Worksheets("Charge Input")
Dim i As Long
i = bdws.Range("M15").Value 'dbws is a worksheet set as a variable, it changes
but the value is always in M15
chargeinput.Range("D8").Formula = "=i-" & D12
Your formula construction is using i as something the worksheet recognizes; concatenate it into the formula. D12 should be D12 on the Charge Input worksheet.
chargeinput.Range("D8").Formula = "=" & i & "-D12"
'does i have to be there?
'is it bdws or dbws? Your sample is confusing
chargeinput.Range("D8").Formula = "='" & bdws.name & "'!M15-D12"
I am inserting the following hyperlink as a formula using vba:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\log.txt"",""View Log"")"
This works fine, however if my value in cell C was to change then my hyperlink becomes invalid and won't work because it has retained the value of the cell at the time in which the formula was entered.
I need a way of making a dynamic reference to my cell C in the event that the value should change then my link will also change.
Can someone please show me how to do this? Thanks in advance
Your code is taking the value from column C and building a string using that value that looks like this:
"S:\Tasks\FolderName\log.txt"
Instead, what you want to do is build the following string:
"S:\Tasks\" & C2 & "\log.txt"
To do that, use this VBA code:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\"" & C" & ActiveCell.Row() & " & ""\log.txt"",""View Log"")"
So I am writing some formulas for my spreadsheets and wondered if there was a placeholder for this row number.
For example say I am using the formula:
=C4+SUM(F4:N4)
I know I can autofill this, but what I really want is some stock formula like:
=C{this.row}+SUM(F{this.row}:N{this.row})
Is this possible? Thanks
The function you need is INDIRECT(CELL/RANGE as string)
The INDIRECT function takes in a string and uses it to evaluate a cell or range location. This is where you get the flexibility (aka indirection) that you need.
eg your formula would be:
=INDIRECT("C" & ROW()) + SUM(INDIRECT("F" & ROW() & ":N" & ROW()))