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
Related
I am trying to write this formula using VBA:
ActiveCell.Value = "=f(R[-1]C[0],Sheet1!" & ColumnLetter & i & ")"
Where ColumnLetter is some variable letter which my macro computes earlier, and f is some function, and i is some number.
The problem is that when I run this, the cell is given this instead: (if ColumnLetter = F, i = 16):
=f(R[-1]C[0],Sheet1!'F16')
but I want:
=f(R[-1]C[0],Sheet1!F16)
Why is VBA or Excel putting those single quotation marks around F16? It does not insert these extra quotation marks if I do not include R[-1][0] as an argument in my formula, but I need to include this.
Help much appreciated!
Its the combination of R1C1 and A1 addressing. You need to pick one method and use it for both parts.
Note that if you type =f(R[-1]C[0],Sheet1!F16) into a cell you will get an error for the same reason.
You say you need to use R1C1 style for the first address, but (assuming this is because you don't want
absolute address) you can use .Offset instead
ActiveCell.Value = "=f(" & ActiveCell.Offset(-1, 0).Address(False, False) _
& ",Sheet1!" & ColumnLetter & i & ")"
The Apostrophe means to Excel that it should interpret it as text.
Write it to ActiveCell.Formula. That way it is recognized as Formula.
I am trying to use VBA to create a description that includes the Month/Year, text, and the value of a cell. I have tried two methods and neither one is working for me.
First i tried this:
ActiveCell.FormulaR1C1 = _
"=CONCATENATE(Format(DateAdd("m", -1, now), "mmmyy"), " My Text ",RC[-19])"
Which gives a
run-time error 1004.
Next I tried
Range("X10").Value = Format(DateAdd("m",-1,now), "mmmyy") & " My Text " & E10
Which output the date and the text but not the value of the cell.
I'm not sure where I'm going wrong with this. Searching on here got me to this point, but I'm stuck.
Thanks for any help.
'Let: X be your Workbook index, Y be your sheet index, YY be your row index, XX be your column index
Sub macro2()
MsgBox Month(Date) & "/" & Year(Date) & " " & Workbooks(X).Sheets(Y).Cells(YY, XX).Value
End Sub
All of these may be stored into a string variable for portability across your project.
Good luck!.
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 & ")"
I'm trying to insert a concatenated string into a cell using VBA. Here's the formula I want to insert:
="ObjectID(" & E152 & ")"
here's what I'm trying, but I can't get it to work:
ActiveCell.FormulaR1C1 = "=""ObjectID("" & RC[-1] & ")"
I've tried "")" and ")"" and a bunch of other combinations, but I cant get it to work.
How do I do this?
here's how I did it:
ActiveCell.FormulaR1C1 = "=""ObjectID("" & RC[-1] & "")"""
Try this:
Sub InputConcatenatedString()
FormulaStr = """ObjectID(""&E2&"")"""
Range("A2").Formula = "=" & FormulaStr '--Modify A2 as needed.
Range("A2:A162").FillDown '--Modify affected range as needed.
End Sub
You just have to change the address of the initial cell as well as the end cell in the desired range. I just assumed that your data starts at A2 and ends at A162. :)
Let us know if this helps.
try this:
Range("A2:A162").Formula = "=""ObjectID("" & E2 & "")"""
Assuming you want to put your values in A2:A162.
Change to whatever range you got.
Hi, i tried to put a formula like ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")" If the country is not India then Check1, Check2 and Check3 should be empty otherwise they should display their own value. when i tried to put that formula the excel has given me circular referencing warning. I just want that formula. Any help would be appreciated greatly.
When a formula refers back to its own cell, either directly or indirectly, it creates a circular reference.
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")"
You are writing the formula in activecell and the formula says if RC[-1]="India"
and if its true RC which is same as activecell. Hence you are getting the circular reference error.
But if you put this formula in Column E as below it would work.
Range("E2:E" & lastRow).FormulaR1C1 = "=IF(RC[-4]=""India"",RC[-3],"""")"
Alternatively below is simple VBA code.
Sub sample()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
For i = 2 To lastRow
If (InStr(1, Cells(i, 1), "India") <= 0) Then
Range("B" & i & ":D" & i).Clear
End If
Next
End Sub
You can't use RC as a result only as this is referencing the current formula.
Option 1: You need to have another cell to be the validated cell, e.g.the following will create a cell to the left one of the current cell:
ActiveCell.Offset(0,1).FormulaR1C1 = "=IF(RC[-2]=""India"",RC[-1],"""")"
Option 2 after comment: If your line of code is only going to clear the current cells value if the left cell is not India then use this:
If ActiveCell.Offset(0,-1).Value <> "India" Then ActiveCell.Value = ""
Option 3: If your default value in RC has to stay but a formula is needed to override it if the value of RC[-1] becomes a not equal to India you must hard code your value in the formula like so:
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",""" & ActiveCell.Value & ""","""")"