Create a Dim that can hold both an Object and a String at the same time?

Jake K

New Member
Joined
Jan 9, 2025
Messages
3
Office Version
  1. 2024
Platform
  1. Windows
I'm trying to create a command button that will copy preset data to clipboard. However, it needs to contain both a String and an Object. For example:

___________
This is the information you requested:


PART DESCRIPTION
3735151BATTERY
__________

Seems like I've tried everything under the Sun but, no luck. Here's as close as I can get:

Private Sub CommandButton32_Click()
Dim grD As Object
Set grD = Worksheets("TMP").Range("A1:B2")

Dim MyData As New DataObject
MyData.SetText "This is the information you requested:" & Chr(10) & grD
MyData.PutInClipboard
End Sub

If anyone can offer a solution, it would be greatly appreciated.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
How about:

VBA Code:
Private Sub CommandButton32_Click()
Dim grD As Range
Set grD = Worksheets("TMP").Range("A1:B2")

Dim MyData As New DataObject
Dim cl As Range
Dim clipboardText As String

clipboardText = "This is the information you requested:" & Chr(10)

    ' Loop through each cl in the range, append to the clipboardText
For Each cl In grD
    clipboardText = clipboardText & cl.Value & vbTab
    If cl.Column = grD.Columns.Count Then
        clipboardText = clipboardText & vbCrLf
    End If
Next cl

MyData.SetText clipboardText
MyData.PutInClipboard
End Sub
 
Upvote 0
How about:

VBA Code:
Private Sub CommandButton32_Click()
Dim grD As Range
Set grD = Worksheets("TMP").Range("A1:B2")

Dim MyData As New DataObject
Dim cl As Range
Dim clipboardText As String

clipboardText = "This is the information you requested:" & Chr(10)

    ' Loop through each cl in the range, append to the clipboardText
For Each cl In grD
    clipboardText = clipboardText & cl.Value & vbTab
    If cl.Column = grD.Columns.Count Then
        clipboardText = clipboardText & vbCrLf
    End If
Next cl

MyData.SetText clipboardText
MyData.PutInClipboard
End Sub
Thanks, ExcelToDAX.

That captures the data in the cells but, doesn't retain the actual cells and formating. It comes out like this when I paste:
__________
This is the information you requested:
PART
DESCRIPTION
3735151
BATTERY
__________

I was trying to figure out how to retain the actual cells:

PARTDESCRIPTION
3735151BATTERY

Thanks for the effort though. Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,225,613
Messages
6,186,004
Members
453,334
Latest member
Prakash Jha

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