XL VBA - Paste Values that actually pastes Values ONLY?

Dont Call me Betty

New Member
Joined
Sep 29, 2023
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Below code does not reliably paste Values only, different formatting gets in.
Tried different approaches, like recording a macro while doing Paste Values.
Nothing works consistently to ONLY paste Values, to match the cell formatting, font, etc.

VBA Code:
Sub Paste()
'Tools -> References -> Microsoft Forms 2.0 Object Library
'or you will get a "Compile error: user-defined type not defined"
  Dim DataObj As New MSForms.DataObject
  Dim s As String
  DataObj.GetFromClipboard
  s = DataObj.GetText
  'Debug.Print S 'print code in the Intermediate box in the Macro editor
  Target.Value = s
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Why are you using the clipboard instead of copying and pasting directly from the cells?
 
Upvote 0
That worked perfectly fine for me.
I copied bold text out of an email and used your macro and it placed in Plain Text.

I wouldn't use Target though, that's used as a passed parameter on Worksheet event code

I would use Activecell
VBA Code:
Sub Paste()
'Tools -> References -> Microsoft Forms 2.0 Object Library
'or you will get a "Compile error: user-defined type not defined"
  Dim DataObj As New MSForms.DataObject
  Dim s As String
  DataObj.GetFromClipboard
  s = DataObj.GetText
  ActiveCell.Value = s
End Sub

but if you're still having problems maybe you can add ClearFormats to the end of it.

VBA Code:
Sub Paste()
'Tools -> References -> Microsoft Forms 2.0 Object Library
'or you will get a "Compile error: user-defined type not defined"
  Dim DataObj As New MSForms.DataObject
  Dim s As String
  DataObj.GetFromClipboard
  s = DataObj.GetText
  ActiveCell.Value = s
  ActiveCell.ClearFormats
End Sub
 
Upvote 0
That worked perfectly fine for me.
I copied bold text out of an email and used your macro and it placed in Plain Text.

I wouldn't use Target though, that's used as a passed parameter on Worksheet event code

I would use Activecell
VBA Code:
Sub Paste()
'Tools -> References -> Microsoft Forms 2.0 Object Library
'or you will get a "Compile error: user-defined type not defined"
  Dim DataObj As New MSForms.DataObject
  Dim s As String
  DataObj.GetFromClipboard
  s = DataObj.GetText
  ActiveCell.Value = s
End Sub

but if you're still having problems maybe you can add ClearFormats to the end of it.

VBA Code:
Sub Paste()
'Tools -> References -> Microsoft Forms 2.0 Object Library
'or you will get a "Compile error: user-defined type not defined"
  Dim DataObj As New MSForms.DataObject
  Dim s As String
  DataObj.GetFromClipboard
  s = DataObj.GetText
  ActiveCell.Value = s
  ActiveCell.ClearFormats
End Sub
I am triggering that macro by:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Tried every combination of your suggestions, to no avail
e.g.: Copying from another XL file where the cell background is yellow into a cell with a background in a different color, in a workbook containing the macro, and cell becomes yellow, instead of keeping own color.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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