Reputation: 710
I want to mock the Format painter behaviour while cycling throug last 8 formatting. For that purpose I store last 8 captured Range in List<> object in cycle format. While applying, I take out each range call COPY method to copy that range on clipboard and then call PasteSpecial method on destination range like below
RangeInfo tempRangeInfo = listRangeInfo[Counter];
//Copy to clipboard
tempRangeInfo.CopiedRange.Copy();
selection.PasteSpecial(xl.XlPasteType.xlPasteFormats, xl.XlPasteSpecialOperation.xlPasteSpecialOperationNone,
false, false);
But if I delete the original range I get:
PasteSpecial method of Range class failed" error.
Any suggestion?
Upvotes: 0
Views: 616
Reputation: 9451
When you copy in Excel (the copied region has the marching ants border) the data isn't actually on the clipboard. Excel uses the clipboard to transfer from the source cells in real-time when you paste. It does this so that it can rework formulas to new relative cell references during the paste operation. If you delete the source cells, then Excel has nothing from which to pull data during the paste operation.
VBA does not have native support to work directly with the clipboard. However, you can add MSForms to the VBA project and then declare an object that does support Clipboard functions.
See the follow web site for instructions and examples how to access the Clipboard directly from VBA:
http://www.cpearson.com/excel/Clipboard.aspx
For me, I did not have MSForms in the list of available references
, as suggested on cpearson
's site, but I did search the C:\ drive for FM20.dll and found it buried in some obscure folder. I copied it to the root of C:\ and was able to add it as a reference
by click browse
from the add references
dialog.
Once MSForms is available, you can use this code to copy text to the clipboard that persists after the source cells are removed.
Public Sub Test()
Dim obj As New MSForms.DataObject
obj.SetText ActiveCell.Value
obj.PutInClipboard
End Sub
This one copies formulas that persist:
Public Sub Test()
Dim obj As New MSForms.DataObject
obj.SetText ActiveCell.Formula
obj.PutInClipboard
End Sub
I don't have time to figure out how to copy the cell's format to the clipboard, but it seems that this is the direction that you need to go, if you want to persist the copied values so you can paste them after the source cells are removed.
Even though the Windows Clipboard can store multiple formats for one item (e.g., RTF and Text versions of a copied selection), it can only store one item at a time.
The Office Clipboard doesn't use the Windows Clipboard. It stores copied values itself, and can store many different selections. However, as of Office 2000, it could not be controlled via VBA (see How to use the Office 2000 Clipboard), and selections copied from Excel are either 'paste text' or 'paste all'. There is no paste special to just paste the formats. I couldn't find anything that says different for later versions of Office.
Conclusion: The only way to use VBA to do a paste special is to have an active / valid / copied selection. So I agree that the only solution is to declare your own persistent global variables (e.g., a list of objects) and store the formats for each copied selection in the list. Since the clipboard really isn't a viable solution, SetData
and SetDataObject
are irrelevant... If it proves impossible to capture the formatting of a cell in a single line of VBA code, you could always enumerate the formats of interest and set flags in your stored objects for later use.
Upvotes: 1