bradyboyy88
Well-known Member
- Joined
- Feb 25, 2015
- Messages
- 562
Hi!
I have some very long sql code that I want to convert into clean lines of 1024 which I can then copy and paste into my vba editor. So I have a userform where i paste the sql code that is in multiple line form and a second textbox which outputs the vba ready code. However, I want to keep the max line under some set amount which we could say is 1000 just to be safe but I did not want to break words up since spaces and new lines can sometimes be weird in SQL. Basically at the 1000th character I want to start looking at the next occurance of a space then insert in a new line break there then start looking again at the next 1000th character set to do the same until I have looped through all the string and now converted into x many rows of 1000 characters. Here is what I have so far but keep getting hung up on how to do this.
I have some very long sql code that I want to convert into clean lines of 1024 which I can then copy and paste into my vba editor. So I have a userform where i paste the sql code that is in multiple line form and a second textbox which outputs the vba ready code. However, I want to keep the max line under some set amount which we could say is 1000 just to be safe but I did not want to break words up since spaces and new lines can sometimes be weird in SQL. Basically at the 1000th character I want to start looking at the next occurance of a space then insert in a new line break there then start looking again at the next 1000th character set to do the same until I have looped through all the string and now converted into x many rows of 1000 characters. Here is what I have so far but keep getting hung up on how to do this.
Code:
Option Explicit
Private Sub CommandButton1_Click()
Dim Str As String
Dim FinalStr As String
Dim n As Long
Dim MaxLength As Long
MaxLength = 900
Str = TextBox1.Text
If Len(Str) = 0 Then
Exit Sub
Else
Str = Replace(Replace(TextBox1.Text, vbLf, " "), vbCr, " ")
For n = MaxLength To Len(Str)
FinalStr = Replace(Str, " ", " " & vbCr & " ", InStr(MaxLength, Str, " "), InStr(MaxLength, Str, " "))
n = n + MaxLength
Next
TextBox2.Text = FinalStr
End If
End Sub