XL VBA - Concatenate Range

Dont Call me Betty

New Member
Joined
Sep 29, 2023
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Below code does it by using cell J1, then copies it.

How to do it without using any cell, straight to the clipboard?

VBA Code:
Sub ConcatSelection()

Dim rng As Range
Dim i As String

For Each rng In Selection
i = i & rng & " "
Next rng

'Range("J1").Value = Trim(i)

Range("J1").Value = (i)
Range("J1").Copy
 
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The post below may help you copy a cell's content to the Windows clipboard.
Hope it helps,

Doug
 
Upvote 0
The post below may help you copy a cell's content to the Windows clipboard.
Hope it helps,

Doug
Not trying to use a cell, that's the solution I am trying to rise away from.
 
Upvote 0
Do you want "i" copied to the Windows clipboard?

If yes...
VBA Code:
        With New DataObject
            .SetText i
            .PutInClipboard
        End With
 
Upvote 0
Do you want "i" copied to the Windows clipboard?

If yes...
VBA Code:
        With New DataObject
            .SetText i
            .PutInClipboard
        End With

With this code I get below error:

1696616215992.png
 
Upvote 0
In VBA editor, Tools, References, put a check by "Microsoft Forms 2.0 Object Library".
 
Upvote 0
In VBA editor, Tools, References, put a check by "Microsoft Forms 2.0 Object Library".

Added that.
Expecting:
test two 123 d456
But get nothing...
Selecting A1:A4
Pasting into notepad gives nothing
Pasting into B5 gives those two little boxes with question marks

1696617328949.png
 
Upvote 0
I recall having a similar issue at some point. Do you have Windows File Explorer open? If yes, try closing that and seeing if you still get the two question marks? I am not 100% sure that is the cause...
 
Upvote 0
I tested this code...
VBA Code:
Sub ConcatSelection()

    Dim rng As Range
    Dim i As String
  
    For Each rng In Selection
        i = i & rng & " "
    Next rng

    With New DataObject
        .SetText i
        .PutInClipboard
    End With

End Sub
The results were...
1696618258052.png


Just to make sure, have A1:A4 selected when you run the code. I hope it works for you...

Doug
 
Upvote 0
I recall having a similar issue at some point. Do you have Windows File Explorer open? If yes, try closing that and seeing if you still get the two question marks? I am not 100% sure that is the cause...
With File Explorer closed, pasting is as expected in both places: NotePad and Excel
Then with File Explorer open, pasting as expected in NotePad, but same issue in Excel
Restarting Excel did not help.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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