משפחה של תוכנות גיליונות אלקטרוניים של Microsoft עם כלים לניתוח, יצירת תרשימים והעברת נתונים.
Please note that this is Hebrew Q&A Forum, any threads should be posted in Hebrew. However, I will still provide an answer on English based on your initial response.
Hi, nativ Lugasi
Welcome to Microsoft Q&A forum.
Sorry for this frustrating situation that you are encountering. VBA (and Office in general) can end up inserting a non‑standard “space-like” character such as a non‑breaking space (Unicode U+00A0, often treated as “CHAR 160”) that looks identical to a normal space but breaks parsing and compilation. This issue can happen when text enters the VBA editor with NBSP characters, and it can also show up as “spaces being removed/shifted” due to editor side effects.
Here are some solutions you can follow to confirm what's being inserted, fix code that already got contaminated, and address the most common root causes on Excel for Mac.
1) Confirm whether the “space” is actually NBSP (U+00A0)
Quick test inside the VBA Editor (Immediate Window)
In the VBA editor, press Cmd+G to open the Immediate Window.
Run this and press Return:
? AscW(" ")
This should print 32 for a normal space.
Now, in a code module, type a “space” using your Spacebar (where the problem happens), copy that character (or copy a short snippet containing it), then in Immediate Window evaluate the copied character (if you can paste it between quotes). If it’s NBSP, you’ll typically see 160 (or &HA0). The key is: normal space is 32, non‑breaking space is 160.
If pasting the character between quotes is finicky, the next section gives a cleanup approach that doesn’t require precise manual insertion.
2) Clean up existing VBA code that already contains NBSP
When NBSP characters are present, VBA can highlight lines red, throw “Compile error: Syntax error”, and behave as if keywords are broken. A standard fix is Find/Replace to swap NBSP with a real space.
In the VBA editor: Find/Replace NBSP > normal space
- Open Find/Replace (Cmd+F, then expand to Replace, or use Edit > Find > Replace).
- In the Find box, try inserting a NBSP:
- If you can copy one of the “bad spaces” from your module and paste it into Find, do that.
- In Replace with, type a normal space.
- Replace All.
If you can’t reliably paste the bad space into Find, use a VBA-based cleanup:
VBA-based cleanup macro (targets NBSP U+00A0)
This approach scans code modules and replaces NBSP with normal spaces. It’s the most reliable way to clean a project after contamination.
Sub ReplaceNBSPInAllModules()
Dim vbComp As Object, cm As Object
Dim i As Long, n As Long
Dim lineText As String
For Each vbComp In ThisWorkbook.VBProject.VBComponents
Set cm = vbComp.CodeModule
n = cm.CountOfLines
For i = 1 To n
lineText = cm.Lines(i, 1)
If InStr(lineText, ChrW(&HA0)) > 0 Then
cm.ReplaceLine i, Replace(lineText, ChrW(&HA0), " ")
End If
Next i
Next vbComp
End Sub
Quick workaround if you need to code immediately (3
If you’re blocked right now and need a stopgap:
- Type code in a plain-text editor that you know produces ASCII space (TextEdit in plain text mode, VS Code, etc.)
- Then paste into VBE and immediately run the cleanup Replace (section 2) if needed.
Also, if the contamination happens during typing but not during paste, this can keep you productive until the underlying cause is resolved.
Hope this helps. Feel free to get back if you need further assistance.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment."
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.