I would like to copy the contents of an excel file to the clipboard, using the same separators and format regardless of user configuration.
Here's my macro:
Private Sub CommandButton1_Click()
'save number separators
Dim d, t, u
d = Application.DecimalSeparator
t = Application.ThousandsSeparator
u = Application.UseSystemSeparators
'set number separators
With Application
.DecimalSeparator = "."
.ThousandsSeparator = ","
.UseSystemSeparators = True
End With
'create temporary copy
ActiveSheet.Copy
'set number format
ActiveSheet.Range("H2:I150").NumberFormat = "0.0000000000"
[...]
'copy sheet to clipboard
ActiveSheet.Range("A1:O150").Copy
'disable messages (clipboard)
Application.DisplayAlerts = False
'close temporary copy
ActiveWorkbook.Close SaveChanges:=False
'reenable messages
Application.DisplayAlerts = True
'reset original separators
With Application
.DecimalSeparator = d
.ThousandsSeparator = t
.UseSystemSeparators = u
End With
End Sub
If I don't reset the original separators at the end, everything works fine, but this is not acceptable for me.
If I do reset the separators (as seen in this code), then the contents of the clipboard are going to have the user specific separators, not the ones I defined at the beginning.
Any ideas on how to fix this?
From Cpearson Site with some modification we can copy any range with custom formats for
Numbers
andDates
to Clipboard with no need to change Excel or System Settings. This module requires a reference to the "Microsoft Forms 2.0 Object Library", we can do this reference by addingUserForm
to the Workbook then we can delete it, (if already there is anyUserForm
in the Workbook we can skip this step).The problem was
setting this to false solves the problem.