I am using a userform for data entry and storing the contents in a cell.
When the form loads it copies the cells contents into the InkEdit box on the form, closing it copies the editied text back into the cell it came from.
For some reason when it copies back into the cell it adds line breaks where there are already line breaks. Loading up the form over and over just adds more and more line breaks!
Does anyone know why this is happening and a possible solution?
Private Sub CMDExit_Click()
Sheets("Comments").Range("B1") = InkEdit1.Text
Unload Me
End Sub
Private Sub UserForm_Initialize()
Sheets("Comments").Range("B1").Copy
PasteToObj InkEdit1.hWnd
Application.CutCopyMode = False
End Sub
Before
After
CodePudding user response:
I just tested your data with this code and it removed the extra line breaks
Sub Sample()
Dim TestString As String
Dim i As Long
TestString = Sheets("Comments").Range("B1").Value
Do While InStr(1, TestString, vbCrLf & vbCrLf & vbCrLf, vbTextCompare)
TestString = Replace(TestString, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
Loop
Sheets("Comments").Range("B1").Value = TestString
End Sub
BEFORE
AFTER
To incorporate it in your code, you can use it like this
Private Sub CMDExit_Click()
Dim TestString As String
TestString = InkEdit1.Text
Do While InStr(1, TestString, vbCrLf & vbCrLf & vbCrLf, vbTextCompare)
TestString = Replace(TestString, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
Loop
Sheets("Comments").Range("B1").Value = TestString
Unload Me
End Sub