How to Copy Format without using Clipboard?

Ceeyee

Board Regular
Joined
Feb 2, 2011
Messages
164
I want to copy the Format of a large range to another cell location using VBA.

I don't want to use .Copy and .Pastespecial because it will interupt with my PC front end copy and paste operations.

I found some code from the internet which does what I want but it's extremely slow as it uses loops to run through each cell.


Is there any way to do it without using the clipboard and looping through each cell to = the format? I am using Excel 2010.

Thanks.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Are the contents of the clipboard (Text only) preserved if the constante XML_SPREADSHEET = 49365 is replaced with XML_SPREADSHEET = 1 ?
 
Upvote 0
Better now!

It stores and restores texts copied from within cells.

But when I clicked a whole cell and clicked copy, then Test() will clear the cell from clipboard.

Also formats are not stored.
 
Last edited:
Upvote 0
Yes, that saves the clipboard data to text format with:
Code:
Public Const XML_SPREADSHEET = 1
I would use a DataObject if a text string was all that we needed. http://www.cpearson.com/excel/Clipboard.aspx

A scatch file would be clunky way to do what you want. A vb.net EXE could store your data and retrieve it but then you would need the framework files.
 
Last edited:
Upvote 0
Yes, that saves the clipboard data to text format with:
Code:
Public Const XML_SPREADSHEET = 1
I would use a DataObject if a text string was all that we needed. http://www.cpearson.com/excel/Clipboard.aspx

A scatch file would be clunky way to do what you want. A vb.net EXE could store your data and retrieve it but then you would need the framework files.

The DataObject woudn't preserve the initial text string if a a copy operation was performed afterwards.
 
Upvote 0
Right, DataObject is fairly limited but for strings alone, it is fine. This is why I did not suggest the DataObject method earlier in the thread.

To see why just saving the text clipboard format won't suffice, download the files in the link, change the path to it in my code, and run this to see the formats.

You may not need to change drive and change dir parts but I added them anyway. If you copy Excel data cells and run this, you may have to run a 2nd time as I did not bother with a shell wait routine. Or just run cbdump.exe from a DOS window after the copy cells in Excel.

Code:
'http://www.mschaef.com/blog/tech/excel/what_is_in_your_clipboard.txt
Sub cbdump()
  Dim sCurDir As String, sCBdump As String, sTXTfile As String
  sCurDir = CurDir
  sCBdump = "X:\Clipboard\cbdump\"
  sTXTfile = "c:\temp\cbdump.txt"
  ChDrive Left(sCBdump, 1)
  ChDir sCBdump
  Shell "cmd /c " & sCBdump & "cbdump.exe > " & sTXTfile, vbNormalFocus
  Shell "Notepad " & sTXTfile, vbNormalFocus
  ChDrive Left(sCurDir, 1)
  ChDir sCurDir
  Kill sTXTfile
End Sub

For two cells copied with one having a red font, the formats were:
c009: "DataObject" (4 bytes)
000e: CF_ENHMETAFILE (0 bytes)
0003: CF_METAFILEPICT (16 bytes)
0002: CF_BITMAP (0 bytes)
c31e: "Biff12" (6083 bytes)
c2da: "Biff8" (15872 bytes)
c2be: "Biff5" (10752 bytes)
0004: CF_SYLK (1521 bytes)
0005: CF_DIF (121 bytes)
c31c: "XML Spreadsheet" (1098 bytes)
c0ba: "HTML Format" (2081 bytes)
000d: CF_UNICODETEXT (14 bytes)
0001: CF_TEXT (7 bytes)
c2d9: "Csv" (7 bytes)
c2e5: "Hyperlink" (199 bytes)
c14e: "Rich Text Format" (3210 bytes)
c00b: "Embed Source" (26624 bytes)
c004: "Native" (26624 bytes)
c003: "OwnerLink" (46 bytes)
c00e: "Object Descriptor" (152 bytes)
c00d: "Link Source" (230 bytes)
c00f: "Link Source Descriptor" (152 bytes)
c2d3: "Link" (61 bytes)
0081: CF_DSPTEXT (13 bytes)
c002: "ObjectLink" (82 bytes)
c013: "Ole Private Data" (728 bytes)
0010: CF_LOCALE (4 bytes)
0007: CF_OEMTEXT (7 bytes)
0008: CF_DIB (37280 bytes)
0011: CF_DIBV5 (37364 bytes)
170352 bytes
 
Last edited:
Upvote 0
Hi Kenneth.

Thanks for the followup.

The DataObject won't preserve even string text if followed by a copy operation. (tried it didn't work)

As for the clipboard format dump , can you try replacing the XML_SPREADSHEET Const in my code with Const XML_SPREADSHEET= &HC31C (Taken from the dump text file) and see if you can preserve the Clipboard contents including the cell Formats.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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