How to fix a VBA set statement failure in MacOS Excel

James Palmer 320 Reputation points
2026-09-14T13:47:49.0333333+00:00

I have the following vba code statements in a MacOS VBA Procedure

Dim wb As Workbook

Dim src As Worksheet

Set wb = ActiveWorkbook

Set src = wb.Worksheets("OrigDL") 'This statement is failing

The second statement is failing and I am struggling to determine what is causing it to fail.

Help appreciated.

Microsoft 365 and Office | Excel | For home | MacOS
0 comments No comments

Answer accepted by question author
Marcin Policht 107.7K Reputation points MVP Volunteer Moderator
2026-09-14T13:54:23.8433333+00:00

The statement Set src = wb.Worksheets("OrigDL") will fail if the workbook referenced by wb does not contain a worksheet named exactly OrigDL. The first thing to verify is which workbook ActiveWorkbook is actually referencing.

Add these lines immediately before the failing statement:

Debug.Print "Active workbook: " & ActiveWorkbook.Name Debug.Print "Workbook path: " & ActiveWorkbook.Path Debug.Print "Worksheet count: " & ActiveWorkbook.Worksheets.Count

You can also list every worksheet name:

Dim ws As Worksheet

For Each ws In wb.Worksheets Debug.Print "[" & ws.Name & "]" Next ws

Then open the Immediate window with Ctrl+G and check whether OrigDL appears exactly as expected. This will also reveal leading or trailing spaces in the worksheet name.

Another potential cause is that ActiveWorkbook is not the workbook you expect. ActiveWorkbook refers to whichever workbook is active when the procedure runs. If OrigDL is in the workbook containing the VBA code, use ThisWorkbook instead:

Set wb = ThisWorkbook Set src = wb.Worksheets("OrigDL")

ThisWorkbook refers specifically to the workbook containing the VBA project, while ActiveWorkbook refers to the workbook currently active in Excel.


If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

hth

Marcin

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Newest

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.