Formula within code

Smythe

Board Regular
Joined
Jan 14, 2010
Messages
101
Hi guys

I am looking for the correct code that will allow me to take data from column A, extract the last 5 characters from the cell and add "980" to the start.
So if cell A1 is ZXZXZ83654, I would like cell B1 to be 98083654.

Does this make sense? I was experimenting with
Code:
ActiveCell.FormulaR1C1 = "="980"& RIGHT(RC[-24))"

but VBA does not approve!

Cheers
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hmmm....oooops....

I forgot to mention that I need the formula to apply all the way down the column - so for every intance of data in column A, column B will have the formula applied?


Cheers
Andy
 
Upvote 0
Hi,

Something like this?

Code:
Sub InsertFormula()
    
    Dim lngLastRow As Long
    
    lngLastRow = Range("A" & Rows.Count).End(xlUp).Row
    
    Range("B1:B" & lngLastRow).Formula = "=(980&RIGHT(A1,5))+0"

End Sub
 
Upvote 0
You can't put that formula in column B because it doesn't have 24 columns to the left of it. To put it in column Y:

Code:
Sub Test()
    Dim LR As Long
    With ActiveSheet
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("Y2:Y" & LR).FormulaR1C1 = "=980&RIGHT(RC[-24],5)"
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top