Inserting formula in a cell

Occasional Contributor

Hello, I'm trying to insert a RIGHT formula in one column but my macros goes into break mode.

Wanted the variable cell to be whatever cell I selected with the mouse before running the macro. Any Advice?

Sub AddFormula()
Dim cell As Range
Set cell = Selection

Range(cell) = Right("*****", 4)

End Sub


6 Replies

Hi @NRASA0743 


you do not need a cell variable. And you need to set the RIGHT-formula in quotation marks.


This should do it:

Selection.FormulaR1C1 = "=RIGHT(""****"",4)"



@Martin_Weiss the 4 desired characters do not appear.

Hi @NRASA0743 


do you get an error or does the makro jump into the break mode?

@Martin_Weiss macro only shows the "*****" but not the last 4 characters. No error or break mode.

best response confirmed by NRASA0743 (Occasional Contributor)

Hi @NRASA0743 


yes, that's because this was the formula that you wanted to put in the cell based on your original question. 


Sorry, now I think I understand: You want only the last 4 characters of whatever is currently in the cell? This should do it:


Selection.Value = Right(Selection.Value, 4)


Please note that this works only for single cells, not for a selection of multiple cells.

Oh ok I see my mistake now. Thank you