KPavezC
KPavezC

Reputation: 305

Trimming text before or after pasting in Excel

I want to paste a string from a TextBox in VB.NET to an Excel Sheet using Clipboard Class:

txtBox.SelectAll()
Clipboard.SetText(txtBox.SelectedText)
Sheet.Range("B5").Select()
Sheet.Paste()

However, the string I obtain finally in my Excel Application is very huge because it ends with multiple and unnecessary spaces and the column I use to pasting it looks very ugly.

There's a way to trim or delete those blank/spaces in the copied string using VB.NET?

Thanks in advance

EDIT: Now I have a new question related. If I have a DataGridView and I suffer from the same problem, how can I trim the text in this case? This is my code:

dgv1.SelectAll()
dgv1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
Clipboard.SetDataObject(dgv1.GetClipboardContent())

Upvotes: 0

Views: 402

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

Check out String.Trim()

Clipboard.SetText(txtBox.Text.Trim)
Sheet.Range.Select("B5")
Sheet.Paste

Upvotes: 1

Related Questions