VBA help to copy text to clipboard

saiyamvora13

New Member
Joined
Apr 3, 2015
Messages
32
Hello,
Here is the Macro that I am using
Sub Selected_to_Comma_Separated()


Dim MyRange As Range
Dim NewRange As Range
Dim arr
Dim clip As MSForms.DataObject

For Each MyRange In Selection
If MyRange.Value <> "" Then
MyRange.Value = Chr(39) & Chr(39) & MyRange.Value & Chr(39)
End If
Next MyRange


Set NewRange = Selection

arr = Join(Application.Transpose(NewRange.Value), ",")

Set clip = New MSForms.DataObject
clip.SetText arr
clip.PutInClipboard

MsgBox "Copied to clipboard: " & Chr$(10) & arr

End Sub

What it does:
Anything that is selected on the excel sheet, it will add quotations in the start(') and end(') and it will add a comma (,) and then copy it to the clipboard
Example (al033j) will become ('al033f',)
Issue is that if I press the macro button the second time, the selection now becomes (''al033f'',)
If i press it 3rd time it becomes ('''al033f''',)
If I press it 4th time it becomes (''''al033f'''',)
This keeps going on.

Is there anyway for me to keep it at ('al033f',) no matter how many times I press the button?

Thank you for all your help
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You might try the following modification...

Code:
[COLOR=#808080]For Each MyRange In Selection
    [/COLOR][COLOR=#0000ff]If MyRange.Value <> "" And MyRange.Value <> Chr(39) & Chr(39) & MyRange.Value & Chr(39) & "," Then[/COLOR][COLOR=#808080]
        MyRange.Value = Chr(39) & Chr(39) & MyRange.Value & Chr(39)
    End If
Next MyRange[/COLOR]

Cheers,

tonyyy
 
Upvote 0
Hi Tonyy, this did not work
When I run the macro a second time, it adds the quotes again to the selection making it ('''al033f''',)

Please let me know if you have a better solution.
 
Upvote 0
saiyamvora13,

I'm assuming when you say "adds the quotes" you really mean apostrophes.

Anyway, the approach below deletes any existing apostrophes before adding new ones.

Code:
Sub Selected_to_Comma_Separated2()

Dim MyRange As Range
Dim NewRange As Range
Dim arr
Dim clip As MSForms.DataObject

For Each MyRange In Selection
    If MyRange.Value <> "" Then
        MyRange.Value = Replace(MyRange.Value, "'", "")
        MyRange.Value = Chr(39) & Chr(39) & MyRange.Value & Chr(39)
    End If
Next MyRange

Set NewRange = Selection

arr = Join(Application.Transpose(NewRange.Value), ",")

Set clip = New MSForms.DataObject
clip.SetText arr
clip.PutInClipboard

MsgBox "Copied to clipboard: " & Chr$(10) & arr

End Sub
 
Upvote 0
You're welcome. Glad it worked out...
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,266
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top