Sewder
Sewder

Reputation: 754

How to format each line in a textbox in VBA Excel

Can I loop through a Textbox for each line entered and format it? For example if the user entered:

Text1
Text2

I would like to output it as in a single line.

'Text1','Text2'

Also would be awesome if you can get the last comma to not display.

Upvotes: 1

Views: 1092

Answers (1)

gaby
gaby

Reputation: 306

This is what you really need

  • Add a textbox to your sheet

  • Add this code on TextBox1_Change() event

    Dim mystr      
    
    mystr = Split(Sheet1.TextBox1.Text, vbCrLf)
    Sheet1.Range("A1") = "'" & Join(mystr, "','") & "'"
    
  • Right click to textbox and enable multiline option

  • Control + Enter to change line inside textbox

And you have the result that you want:

enter image description here

Upvotes: 3

Related Questions