I have what seems like a simple problem with some data in Excel. I have a lot of data with leading spaces pasted from a web table, and I would like to get rid of the initial
The infinite loop is a result of an On Error Resume Next
statement somewhere higher up in your code. Get rid of it now. If your code only runs with On Error Resume Next
in effect, it needs instant and complete overhaul.
And while you are at it, place an Option Explicit
on top of yor module. It forces you to declare all variables, a safeguard against annoying bugs that are the result of mis-typed variable names. (You can modify the VBA editor preferences to have that option set automatically the next time, which is highly recommended.)
The immediate problem with your code is that a cell is an object, and objects cannot be trimmed. You want to trim the text of the cell, so you must do so:
cell.Text = Trim(cell.Text)