Q:

Modifying cells in Excel with a VBA function

Every cell modifying line in the code below "throws" a 1004 error.
The following Function is called through a cell, this way: =bonjour()
Here is the code: 
Public Function bonjour() As Integer
On Error GoTo Handler
    Range("B2").Value = 41
    Cells(2, 2) = 42
    ThisWorkbook.Sheets("Feuil1").Range("B2").Value = 43
    ThisWorkbook.Sheets("Feuil1").Cells(2, 2) = 44
Handler:
    If Err.Number <> 0 Then
        Debug.Print ("Error n° " & Err.Number & " - " & Err.Description)
        Err.Clear
        Resume Next
    End If
    bonjour = 45
End Function

A:

You cannot modify cells of the worksheet using a UDF (User Defined Function) called from the worksheet. Sorry. Excel will not allow this. Attempting it will cause an error every time. 
From Microsoft Website :

A user-defined function called by a formula in a worksheet cell cannot
  change the environment of Microsoft Excel. This means that such a
  function cannot do any of the following:

Insert, delete, or format cells on the spreadsheet. 
Change another cell's value. 
Move, rename, delete, or add sheets to a workbook.
Change any of the environment options, such as calculation mode or    screen views. 
Add names to a workbook. 
Set properties or execute most methods.

You need to find another way to call bonjour. Maybe using a button/shape or perhaps using Events.

