Using Sheets.Activate method within a VBA function doesn't work. Why?

Pixley, Ray 1 Reputation point
2022-07-11T12:15:34.537+00:00

The function Sheets.activate doesn't work within a VBA function call. What am I doing wrong?

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,720 questions
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Pixley, Ray 1 Reputation point
    2022-07-12T18:06:11.787+00:00

    Ok. Tried the call-a-sub approach approach already with no success before posting the question.
    However, here's some code showing what I tried.

    But first create an excel file with at least two sheets, name one "Help Sheet" and the others something else.
    Put in any cell in one of the other spreadsheets the formula: =ActivateThis("Something")
    and let it calculate, preferably stepping it using F8.
    See if it delete the "Help Sheet" when the sub finds it. It will find the sheet, but won't delete it.

    The eventual plan is to include it as part of an add-in, a button won't do.

    Function ActivateThis(eInput As Variant) As Variant
    ActivateThis = "Whatever" ' only because the function apparently needs to return something
    Call ActivateHelpSheet
    End Function

    Sub ActivateHelpSheet()
    Dim ws As Variant
    For Each ws In Sheets
    If ws.Name = "Help Sheet" Then ws.Delete
    Next ws
    End Sub

    Why won't it delete? Or if .activate is use, why won't it activate? Ditto with .select? Another downstream issue is it not allowing cells to be filled with data, not allowing sheets to be added, even when called via subs, but those questions are beyond the scope of this question.


  2. Pixley, Ray 1 Reputation point
    2022-07-12T18:14:42.49+00:00

    To add to the above, I also tried

    If ws.Name = "Help Sheet" Then ws.Activate
    If ws.Name = "Help Sheet" Then ws.Delete

    to see if the sheet had to be activated before deleted. No joy there wither.

    0 comments No comments