Question : Crash when using VLOOKUP in MS-Excel

I do the following in MS-Excel:
1) Create an Excel add-in (.xla) file.  I named it testing.xla.
2) In the 'ThisWorkbook' of this file, copy and paste the following VBA codes:

Private Sub PasteFunction()
    If Not ActiveCell.HasFormula Then
        'Call the default action of the 'Paste function' button
        ActiveCell.FunctionWizard
    ElseIf IsMyFunction(ActiveCell.Formula) Then
        MyFunction
    Else
        'Call the default action of the 'Paste function' button
        ActiveCell.FunctionWizard
    End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ctl As CommandBarControl
    Set ctl = Application.CommandBars.FindControl(, 385)
    If Not ctl Is Nothing Then ctl.OnAction = ""
End Sub

Private Sub Workbook_Open()
    Dim ctl As CommandBarControl
    Set ctl = Application.CommandBars.FindControl(, 385)
    If Not ctl Is Nothing Then ctl.OnAction = "ThisWorkbook.PasteFunction"
End Sub

Private Sub MyFunction()
    MsgBox "MyFunction"
End Sub

Private Function IsMyFunction(ByVal sFormula As String) As Boolean
    IsMyFunction = True
End Function

3) Basically the above codes do the following things:
- find the button 'Paste function' in MS-Excel (which has id = 385) by using the method FindControl.  
- then change the default action of the button by using the property OnAction, so that it will call the customized function PasteFunction instead of its own default action.
- in the function PasteFunction, it is checking whether the active cell contains a customized formula.  If it does not, then call the default action of the 'Paste function' button.

4) Now after I've installed this add-in file by selecting it in the menu 'Tools, Add-ins', I create a new workbook in MS-Excel.  

5) Click on any cell in the workbook.  Then click on the button 'Paste function' in the toolbar.  Select the function 'VLOOKUP'.

6) In the field 'Lookup_value', click on the button on the right of the field to select a range.  Then click on cell A1 in Sheet 1.  Notice the field now contains 'Sheet1!A1'.

7) For the next field 'Table_array', click on the button on the right of the field to select a range.  This time click on cell A1 in Sheet 2.  Notice the field now contains 'Sheet2!A1'.

8) Now click on OK button.  MS-Excel crash.

It happens in both Office 97 and Office 2000.
In Office 2000, I can solve it by using the following method instead of the onAction property:

Public WithEvents cmdPasteFunction As CommandBarButton

Private Sub Workbook_Open()
   Set cmdPasteFunction = Application.CommandBars.FindControl(, 385)
End Sub

Private Sub cmdPasteFunction_Click(ByVal ctrl As CommandBarButton, CancelDefault As Boolean)
   If IsMyFunction(ActiveCell.Formula) Then
       CancelDefault = True
       MyFunction
   End If
End Sub

But this statement is not supported in Office 97.
Public WithEvents cmdPasteFunction As CommandBarButton

Does anyone know how to solve this in Office 97?

Another question is: how do I make the menu 'Insert, Function...' in MS-Excel to call my own customized function in VBA?

Answer : Crash when using VLOOKUP in MS-Excel

Try this, it seems to work on Excel 97 SR-2


Private Sub PasteFunction()
  With ActiveCell
    If .HasFormula And IsMyFunction(.Formula) Then
       MyFunction
    Else
      On Error Resume Next
         'Call dialog directly, as FunctionWizard method seems to be buggy in Excel 97
         Application.Dialogs(xlDialogFunctionWizard).Show
         If Err.Number <> 0 Then
            'Placeholder for error handler
            MsgBox "FunctionWizard called from " & .Address & " returned error number " & Err.Number
         End If
      On Error GoTo 0
    End If
  End With
End Sub
Random Solutions  
 
programming4us programming4us