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
 
I do not know of any fix for it. It may be a known Windows bug...I know it bugs me:) Hopefully, you don't need the File Explorer open.

Edit: there might be a workaround here...https://www.reddit.com/r/vba/comments/i0pnmk/why_am_i_getting_two_boxes_with_question_marks_on/?rdt=48314

Doug
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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...
View attachment 99877

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

Doug
Of course those are selected. And the code is the same. But, guessing you don't have the File Explorer open when it is a success?
Need to have it work even when File Explorer is open.
 
Upvote 0
There is an issue with using the DataObject to paste to the Windows clipboard. It does not work if there is more than one instance of Windows Explorer open. Try this alternate method.
VBA Code:
Sub ConcatSelection()
    Dim rng As Range
    Dim i As String
    Dim CopySuccess As Boolean

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

    ClearClipBoardText
    CopySuccess = SetClipBoardText(i)
End Sub

Private Function ClearClipBoardText() As Boolean
    ClearClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.clearData("Text")
End Function

Private Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function
 
Upvote 1
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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