A function that uses other cells cannot become a formula function (UDF) in Excel, because it breaks Excel's dependency model (all cell dependencies must be explicit in the formula). However a formula can use cell values as inputs.
Here is a simple function added to a module in VBA:
Public Function testFunction(inputValue As Integer) As Integer
testFunction = inputValue * 2
End Function
This can be used in any cell formula. For example, =testFunction(4)
or =testFunction(A5)
.
EDIT
Okay, reviewing the comment you made against guitarthrower's answer. A formula can only send an answer to the cell it is in - it cannot send an answer to a different cell. Therefore, if a formula is your only choice, you must have a formula in cell A1 that reads input from C1.
Formula in A1:
=a(C1)
Function in module:
Public Function a(string col)
a = iif(col = "ok", "1", "2")
End Function
However, if there is a problem putting a formula into A1, you are left with a manually driven process (a sheet button, a toolbar button etc. to push these values) or a worksheet cell change event. The downside of a worksheet event is that it fires off every single cell change, so you need to keep the code light and not do any heavy duty work - or if you do, make it rare.
You would add a new subroutine to the worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
This is fired whenever cells are changed; the changed cells are indicated by the Target
range. Your code would update A column cells if the Target contained C column cells. This is more work than the formula approach, but it does render the process entirely automatic. I have no access to Excel right now so the following is just from memory and Googled fragments, untested:
For Each cCell In Application.Intersect(Target, Me.Range("C1:C5"))
cCell.Offset(, -2).Value = iif(cCell.Value = "ok", "1", "2")
I would prefer not to use explicit ranges, but rather a named range as it makes your code less fragile to change. But that would make finding the corresponding A column cell a little more tricky to determine and I'm not going to attempt that code without Excel to hand =)