Currently, I am having a formula in my cell:

```
=IFERROR(VLOOKUP(A:A,'Daily Report'!A:Z,2,FALSE),"")
=IFERROR(VLOOKUP(A:A,'Daily Report'!A:Y,7,FALSE)&", "&VLOOKUP(A:A,'Daily Report'!A:Y,8,FALSE)&", #"&VLOOKUP(A:A,'Daily Report'!A:Y,9,FALSE)&"-"&VLOOKUP(A:A,'Daily Report'!A:Y,10,FALSE)&", Singapore "&VLOOKUP(A:A,'Daily Report'!A:Y,11,FALSE),"")
```

How do I convert it to VBA so the entire column will be encrypted with this formula?

My formula is always replaced by the guys using my excel sheet.

I am avoiding locking the cell, hence looking at VBA to perform this action.

Edited:

**MACRO**

```
Sub vlookup()
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(VLOOKUP(C[-3],'Daily Report'!C[-3]:C[22],2,FALSE),"""")"
Selection.AutoFill Destination:=Range("D2:D" & LR), Type:=xlFillDefault
End Sub
```

Now how to make data e.g. 09-02-18022013-03383-A when enter to column A, it will run the macro to input the rightful data.

If you must use VBA, the simplest way would be to just re-write the formula in the affected cells:

First, place this in the Worksheet’s module. This will cause the macro to fire EVERY time a change is made to the column A.

```
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target,Me.Range("A:A")) Is Nothing Then
Application.EnableEvents = False 'to disable infinite loop
InsertFormula
Application.EnableEvents = True
End If
End Sub
```

Then, place this in an ordinary code module:

```
Sub InsertFormula()
Dim rng as Range 'this will set the range in which you want this formula to appear
Dim cl as Range 'cell iterator within rng variable
Dim strFormula1 as String `string to hold the formula text
set rng = Range("B2:B39") 'Change this range to the desired range
strFormula = "=IfError(Vlookup(A:A,'Daily Report'!A:Z,2,False),"")"
For Each cl in rng
cl.Formula = strFormula
Next
End Sub
```

So, programmatically inserting a normal formula is fairly easy.

The question then becomes how often do you want to force/overwrite these cells? You can tie this macro to “events” like, whenever the workbook file is opened, or whenever a value on the sheet changes, or whenever someone manually changes the cells you don’t want them to change, etc.

Your second formula you could do the same thing with it, just add another Range variable (e.g., `Dim rng2 as Range`

) and another string variable to hold the formula text (e.g., `strFormula2`

).

Alternatively, you could “rewrite the formula” purely in vba. Replace `cl.Formula = strFormula`

with `cl.Value = MyLookupFormula`

and add this function to the code module containing the subroutine above:

```
Function MyLookupFormula() as Variant
'Performs equivlanet to worksheet function
If Not IsError(Application.WorksheetFunction.Vlookup(Range("A:A"),Sheets("Daily Report").Range("A:Z"),2,False)) Then
myLookupFormula = (Application.WorksheetFunction.Vlookup(Range("A:A"),Sheets("Daily Report").Range("A:Z"),2,False))
Else: myLookupFormula = vbNullString
End Function
```

But that requires knowing more about how often/what events will trigger this macro, since the cells will not have any formula (the formula/computation is performed in memory ONLY when requested by user or an event trigger).

Tags: excelexcel, vba