Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an alternative way of doing what you are trying to accomplish. I find it gives you more control than <code>Find</code> and <code>Replace</code>.</p> <pre><code>' Get the contents of the cell Dim s As String s = Range("A1").Value ' Now write back only what precedes the double space Range("A1").Value = Left(s, InStr(s, " ") - 1) </code></pre> <p>The above operates on only one cell. To do the same over many cells, you could do this:</p> <pre><code>Dim cell As Range For Each cell In Range("A1:A3") cell.Value = Left(cell.Value, InStr(cell.Value, " ") - 1) Next cell </code></pre> <p>As has been pointed out in other answers, you should replace any troublesome non-break spaces (<code>Chr(160)</code>) by regular spaces, prior to searching for double spaces: </p> <pre><code>Dim cell As Range For Each cell In Range("A1:A3") cell.Value = Left(cell.Value, _ InStr(Replace(cell.Value, Chr(160), " "), " ") - 1) Next cell </code></pre> <p><strong>EDIT</strong> Addressing @chris neilsen's comment:</p> <p>If some of your target cells are devoid of double spaces, then you should check for that prior to using the <code>Left</code> function lest it raise an error: </p> <pre><code>Dim cell As Range Dim i As Long For Each cell In Range("A1:A5") i = InStr(Replace(cell.Value, Chr(160), " "), " ") If i &gt; 0 Then cell.Value = Left(cell.Value, i - 1) End If Next cell </code></pre> <p>Now, on the very remote chance that some of the target cells contain formulas comprising double spaces (e.g. <code>=A1 &amp; "&lt;space&gt;&lt;space&gt;" &amp; A2</code>), these formulas will be replaced by values. To avoid this, change the conditional to <code>If i &gt; 0 And Not cell.HasFormula Then</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload