Share via

When running VBA code as I step through it goes to a function that is not called and there are no on events set either

Helen W 0 Reputation points
2026-03-27T15:30:02.5366667+00:00

I have an array that I have calculated and when I am writing it to Excel as you step through the code it correctly pastes the data as it goes through the yellow line the information is showing in the window below and that is what is correctly pasted. After it pastes it does not go to "STOP" on the next line, It then goes to a function that is not called (which is nothing to do with pasting data it just collects the users windows login details) User's image

This is the function it goes to:

Function UNAmeWindows() as String

UNameWindows = Environ("USERNAME")

End Function

After that is pastes "CORE" everywhere then goes to the stop line.

If I run it a second time it doesn't go to the Function and pastes properly. I have a workbook with about 200 sheets where I am removing formulae with some criteria and keeping others. This works consistently an the majority of the sheets, but 3 of the sheets always fail in the way described above.

I am wondering what are the conditions that would make a function run when it not called in the code. The only Workbook Events I have code for in the book are on Open and BeforeClose. But apart from that that function isn't pasting any data and it's not pasting the value obtained from the Function just unrelated rubbish.

If there is a reason it is doing this why would it only do this for 3 sheets where the other sheets are very similar in structure.

Developer technologies | Visual Basic for Applications
0 comments No comments

2 answers

Sort by: Most helpful
  1. Taki Ly (WICLOUD CORPORATION) 460 Reputation points Microsoft External Staff Moderator
    2026-03-30T09:56:13.0833333+00:00

    Hello @Helen W ,

    This could be related to how Excel recalculates the workbook, rather than the paste line directly calling UNAmeWindows(). Microsoft explains that Excel keeps a dependency tree and calculation chain, so when worksheet data changes, Excel may recalculate formulas that depend on it. Microsoft also explains that VBA worksheet functions can be recalculated by Excel, depending on whether they are volatile or nonvolatile.

    In other words, a VBA function can still run even when it is not called directly by the current procedure if Excel is recalculating something that depends on that function somewhere else in the workbook.

    So, if UNAmeWindows() is used anywhere in the workbook, for example in a cell formula, a defined name, conditional formatting, or data validation, and then stepping over OutRange = vInArrayF could make Excel recalculate and enter that function, even though your code does not call it directly.

    Because this only happens on 3 sheets, I would suggest checking whether those sheets are different in some hidden way. For example:

    • UsedRange
    • merged cells
    • leftover formulas
    • sheet-level names
    • validation rules

    Also, UNAmeWindows() only returns the Windows username, so it would not directly explain why "CORE" is pasted everywhere. That part may be coming from the array itself or from the target range not matching the array correctly.

    I would suggest:

    1. Search the workbook for any use of UNAmeWindows().
    2. Compare the 3 failing sheets with one working sheet.
    3. Check that vInArrayF and OutRange have exactly the same size.
    4. Try setting calculation to manual before writing the array.

    You could try checking the points above to see if any of them helps narrow it down. If you find anything new, please feel free to comment below and I’ll be happy to help further. If this answer is helpful, you can also follow this guide to leave feedback.

    Thank you.


  2. Q&A Assist
    2026-03-27T15:30:30.01+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    In Excel VBA a function can run without being explicitly called in code in these main situations:

    1. The function is used in a worksheet cell formula (a user‑defined function/UDF).
    2. The function is used in a defined name (Name Manager), conditional formatting, data validation, or similar formula-based feature.
    3. The function is used in an event procedure (for example, called from Worksheet_Change, Worksheet_Calculate, Workbook_Open, etc.).
    4. The function is used in another UDF that is itself called from a worksheet formula or name.

    When stepping through VBA, Excel’s recalculation can cause any UDF referenced in the workbook to be evaluated, even if the current procedure never calls it. This matches the behavior described: while writing values to the sheet, Excel recalculates; if any cell formula, name, or conditional format refers to UNameWindows, Excel calls that function. Because recalculation can occur multiple times and with intermediate values, the function may be called in ways that appear unrelated to the current code, and only on sheets where such formulas or names exist.

    To diagnose and fix:

    1. On each of the three problematic sheets:
      • Search all cell formulas for UNameWindows(.
      • Check Name Manager for names whose RefersTo formula uses UNameWindows.
      • Check conditional formatting and data validation formulas for UNameWindows.
    2. Remove or adjust those references if the function should not be used as a worksheet UDF, or change the function name so it cannot be referenced accidentally.
    3. If UNameWindows must remain a UDF, ensure that any formulas using it do not interfere with the ranges being written by the array, or move those formulas to a different sheet.

    This behavior is consistent with how Excel treats user‑defined functions: they are recalculated as part of the workbook’s calculation process and can be invoked multiple times with intermediate or unexpected arguments while other VBA code is running.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.