Excel VBA: Getting rid of spaces from code copied from the internet

Anonymous
2017-10-28T19:38:59+00:00

Hello,

Lately when I paste code from the internet (various sites) to Excel VBA module, the body of the code will be red text, and if I try to run it it will say Compile Error: Syntax Error. I have to manually remove the spaces at the beginning and end of each line of code and in between lines of code for it to run.

Why is this happening so often lately and what is a quick way to remove the unwanted spaces described above? 

Thank you for your help.

Here is a picture:

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2017-10-28T19:59:59+00:00

    Hi KC_C,

    Your problem is due to the presence of non-breaking space characters (Ascii 160).

    To remove them from your code:

    • Select the code
    • Ctrl+H to open the Find\Replace dialog
    • In the Find box,, with the Alt key depressed, type 0160 using the numeric keypad
    • In the replace Box, delete any text
    • Replace all
    • OK

    ===

    Regards,

    Norman

    2 people found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-10-28T21:02:18+00:00

    Thank you, Norman. That sold the problem. God bless you.

    0 comments No comments
  2. Anonymous
    2017-10-28T21:14:11+00:00

    Hi KC_C,

    Thank you, Norman. That sold the problem. God bless you.

    Thank you for your kind feedback and I am glad that you have resolved the problem. 

    ===

    Regards,

    Norman

    0 comments No comments
  3. Ashish Mathur 100.9K Reputation points Volunteer Moderator
    2017-10-29T00:17:45+00:00

    Hi,

    You may refer to my solution here.

    Hope this helps.

    0 comments No comments
  4. Anonymous
    2017-10-29T15:08:50+00:00

    Hi,

    You may refer to my solution here.

    Hope this helps.

    Thank you.

    0 comments No comments