title: Reducing number of Line Breaks/chr(10) in a Cell
body: <p>I have an excel sheet that has cells with variable amounts of line breaks and I want to reduce it so that there is only one line break between each new line.</p>
  
  <p>For example</p>
  
  <pre><code>HELLO
  
  
  
  
  WORLD
  
  
  
  GOODBYE
  </code></pre>
  
  <p>will be modified to:</p>
  
  <pre><code>HELLO 
  
  WORLD
  
  GOODBYE
  </code></pre>
  
  <p>I've been banging my head over this for hours and have come up with a few ways but none are very efficient or produce the best results. </p>
  
  <p>This is made especially difficult because I'm working with a dataset that has spaces preceeding the Line Breaks. </p>
  
  <p><a href="https://i.stack.imgur.com/9KdZv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9KdZv.png" alt="enter image description here"></a></p>
  
  <p>And so a regular parse doesn't work as well.</p>
  
  <p>I've tried to replace all the instances of <code>chr(10)</code> in the cell with <code>~</code> to make it easier to work with, however i'm still not getting it to an exact amount. I'm wondering if there are better ways.</p>
  
  <p>here is what I have so far:</p>
  
  <pre><code> myString = Replace(myString, Chr(10), "~")
  
      Do While InStr(myString, "~~") &gt; 0
          str1 = Split(myString, "~")
          For k = 0 To UBound(str1)
          myString = Replace(myString, "~~", "~")
          Next k
      Loop
  
      Do While InStr(myString, "   ~") &gt; 0
          str1 = Split(myString, "~")
          For k = 0 To UBound(str1)
          myString = Replace(myString, "  ~", "")
          Next k
      Loop
  
  myString = Replace(myString, "   ~", " ~")
  myString = Replace(myString, " ~", "~")
  myString = Replace(myString, "~", Chr(10))
  
  Cells(2, 2).Value = myString
  </code></pre>
  
  <p>So i'm using a few do while loops to catch instances of different types of line breaks (or in this case, tildes) but I don't think this is the best way to tackle this.</p>
  
  <p>I was thinking of ways to loop through the characters in the cell, and if there is an instance where there is more than one chr(10), replace it with <code>""</code>.</p>
  
  <p>So the psuedocode would look like:</p>
  
  <pre><code>for i to len(mystring)
      if mystring(i) = chr(10) AND myString(i+1) = chr(10) Then
         myString(i + 1) = ""
  </code></pre>
  
  <p>but unfortunately I don't think this is possible through vba. </p>
  
  <p>If anyone is kind enough to help me adjust my current code or assist me with the aforementioned psuedocode, it would be greatly appreciated!</p>