Sharid
Well-known Member
- Joined
- Apr 22, 2007
- Messages
- 1,066
- Office Version
- 2016
- Platform
- Windows
Hi
Could someone please help me in improving my code below. I use it to paste URLs from a TextBox ("TextBoxPaste") to a sheet and then the results show in the listbox.
The code allows me to paste multiple rows on URls into the textbox which then are placed in Sheet7 A2 down 1 url per row. THIS BIT IS FINE
My Problems are as such. See image attached, Textbox is shown in yellow.
I need the code to runs as such
Could someone please help me in improving my code below. I use it to paste URLs from a TextBox ("TextBoxPaste") to a sheet and then the results show in the listbox.
The code allows me to paste multiple rows on URls into the textbox which then are placed in Sheet7 A2 down 1 url per row. THIS BIT IS FINE
My Problems are as such. See image attached, Textbox is shown in yellow.
- Even if the textbox is NOT selected and the user click on the button "Paste from Clipboard", the URLs are pasted in.
- Even if there is NO copied URLs in the clipboard the following message pops up MsgBox "Your Copied Urls Have Been Pasted Now Click Ok and then Start"
I need the code to runs as such
- The user clicks inside the text box, then
- The user click on the button "Paste from Clipboard", If NO Urls are copied then message = "There are NO urls to paste, please copy some first then try again"
- If there are URLs copied then it runs the code and final message MsgBox "Your Copied Urls Have Been Pasted Now Click Ok and then Start"
VBA Code:
Private Sub CommandButton1_Click()
'Pastes from ClipBoard
Dim objdataobject As MSForms.DataObject
Set objdataobject = New MSForms.DataObject
Dim Str As String, a
Dim cnt As Integer
Dim w()
objdataobject.GetFromClipboard
If Me.TextBoxPaste.Value = "" Then
On Error Resume Next
Me.TextBoxPaste.Value = objdataobject.GetText
Str = TextBoxPaste.Value
a = Chr(10)
cnt = UBound(Split(Str, a))
On Error Resume Next
ReDim w(1 To cnt + 1, 1 To 1)
For i = 0 To cnt
w(i + 1, 1) = Split(Str, Chr(10))(i)
Next i
Sheet7.Range("A2").Resize(i, 1) = w
TextBoxPaste.Value = ""
MsgBox "Your Copied Urls Have Been Pasted Now Click Ok and then Start"
Exit Sub
End If
End Sub