VB Excel PasteSpecial requiring clipboard content? - excel

I'm having a problem with VB PasteSpecial.
This code works perfectly in Excel VB (given that you have selected cells with data)
Selection.Copy
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
However, I'm using a third-party software (QlikView) that I extract the data from, which is then supposed to be copied into the Excel document. There is no problem with the normal paste but it MUST be transposed.
Obviously, since I dont have any content in the workbook to copy, I don't use
Selection.Copy
But because I don't copy anything from the document first (even though there are table data in the copy memory), this call returns bad argument exception (this also happens if I copy cells in that VERY workbook first and then just call the macro for transposing it).
Runtime error '1004' returned. PasteSpecial method of Range class failed.
Yes, I can paste it into the document, then cut it from the area, move it to the correct place and transpose it, but that is bad coding.
Have any of you experienced this and got a way to get this working ?

You will have to use the method as you mentioned above. You can also try this
Range("A1").Select
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
Application.CutCopyMode = False
But you will have to copy it again and transpose it. Other wise there is no direct way you can transpose it.

The reason that you cannot use PasteSpecial to transpose the data is due to the format of the data as it resides in the clipboard after you copy it from QlikView.
When you copy data from a QlikView table (which I assume you are copying from), it copies it to the clipboard in three formats: HTML, Unicode and standard (code-paged) text:
Comparing this with Excel's clipboard formats:
As you can see, when copying data in Excel, it stores the data in the clipboard in its own format and as such knows how to transpose the cells if required. For QlikView, the clipboard just contains plain text, therefore Excel does not know how to transpose this and as a result the PasteSpecial call fails.
If you are copying from a table in QlikView to Excel, I would recommend performing the transposition already in QlikView if you can by using a "Pivot Table" chart in QlikView (as you can drag the columns and rows around how you wish). Otherwise you will have to use Siddharth's code and transpose it once it's in Excel.

Related

Copying over drop downs

I have a VBA macro that copies over formulas and values from one sheet to another one. The problem is that when it copies over dropdown lists, it deletes the list and just copies over the value.
I tried both of the below, but both have the same problem
wb_to.Sheets("Core").Range(cel_loc_new).PasteSpecial Paste:=xlPasteValidation, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
and
wb_to.Sheets("Core").Range(cel_loc_new).PasteSpecial Paste:=xlPasteAll
What am I doing wrong?
The answer is instead of using .Copy one has to do = .Value. So instead of copy and pasting you have to adjust the value of the cell.

How to prevent Excel VBA paste special values from changing data type from number/date to text

I am encountering a problem where Excel VBA paste special values is changing the data type to text when it carries out the paste values operation, which then breaks downstream formulas that expect to see a number/date instead of text.
At a high level, the process I have is the following:
starting with functional worksheet, duplicate and rename it;
perform operations on the newly created sheet including copy, paste special values;
formulas that depend on the pasted data are now broken because data type has been changed to text.
The code that is doing the copy paste special values looks like this:
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(refreshCopyRange).Copy
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(pasteRange).PasteSpecial (xlPasteValues)
Is there some kind of modifier or override to PasteSpecial (xlPasteValues) that will stop Excel VBA from changing the data type?
Many thanks in advance for any help!
You need to do a second paste command for formatting:
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(pasteRange).PasteSpecial xlPasteValues
Workbooks("myFile.xlsm").Sheets(pageFocus).Range(pasteRange).PasteSpecial xlPasteFormats
Here is a sample:
https://social.msdn.microsoft.com/Forums/office/en-US/b593e52c-910c-4d24-b738-65878fe8a50d/how-to-copypaste-range-values-and-formats?forum=exceldev
You can use this
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False

Excel VBA Error "The picture is too large and will be truncated" when closing file

I have been working on this project for a long time and am suddenly getting a new error whenever I close my Excel file. I get the error twice "The picture is too large and will be truncated." There is no picture in my file. I am pasting formats.
This seems to be one of the Excel "Unsolved Mysteries".
I am using MS Office Professional Plus 2010 on Windows 7.
I have researched this and tried the following:
Deleted all %temp% files
Ran CCleaner
Set CutCopyMode =
False after all paste special (formats)
Went to add/remove
programs and reconfigured Office to stop the Clip Organizer from
running. (Control Panel\Programs\Programs and Features -> MS Office
Professional Plus 2010 -> Change -> Add or Remove Features -> Office
Shared Features -> Clip Organizer -> Not Available, etc.)
Rebooted
None of that helped, so I narrowed down the source of the problem by commenting out function and subroutine calls, running the program, saving and then pressing "x" to close. I did this until I found the right sub. Then I commented out all the lines of the sub and added them back in one logical chunk at a time until I found the problem area. Here it is:
' *********** APPLY BASIC ROW FORMATTING FROM TEMPLATE ***********
' Copy basic row formatting from template and paste over all rows
wksTemplate.Rows(giHEADER_ROW + 1).Copy
myWS.Rows(lFirstRow & ":" & lLastRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
The paste contains formatting only - colors, borders, number formats, wrapping etc. It probably pastes on a range of 200 rows on average.
I have not changed these 3 lines of code in months. Why now?
Has anyone solved this mystery?
Thanks,
Shari
I got this error after copying a range and then using a set of pastespecial calls:
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteValuesAndNumberFormats
.PasteSpecial xlPasteFormats
solution was to copy an empty cell and pastespecial xlvalues back into itself:
' to avoid the message on closing the book - "picture is too large and will be truncated", copy and paste a singe empty cell
ThisWorkbook.Worksheets(1).Cells(1, 1).Copy
ThisWorkbook.Worksheets(1).Cells(1, 1).PasteSpecial xlValues
' clear clipboard
Application.CutCopyMode = False
I sometimes have the same problem as you, but i have many pictures in my files...
Also sometimes i have slow down (open/close or just standard calculations(menu popup...)).
Usually when i close the workbook, and reopen it, it works fine again.
I have maybe some answer, not sure if any help :
Do you use Global variables ?
for example, in module 1:
Option Explicit
Public BigObject as AnyBigSizeType
Sub xxx() 'code following
try to avoid using global variables, usually its a mess and not even usefull.
Also, just to be safe, try in the immediate window : activesheet.pictures.delete
(or even activesheet.shapes.delete , but this one also deletes comments and other stuff...)
I get the error when closing a file after a macro used the Range.Copy command, even if the clipboard is empty. Doing Application.CutCopyMode = False is not enough, but adding this at the end of macros that use the Range.Copy method seems to be the solution:
This is my solution:
'without this it will say "The image will be truncated because it's too large", because Excel is stupid
[A1].Copy
Application.CutCopyMode = False

Automating copying data from one Excel file to another

Ok this is what I am trying to achieve. I have 50,000 items I have in a spreadsheet I need to select 999 items out of one column copy them and paste them to a different column in a different file then have it go to the next 999 items all the way down to the last one.
How can I automate this?
Three possible methods:
VBA (trivial)
C# (see e.g. this)
Jet OLEDB Provider + SQL (see e.g. this)
There is an example here - MS Excel: Copy range of cells from one sheet to another sheet matching on date values in Excel 2003/XP/2000/97 that shows how to use VBA to automate this.
I'm not posting the example but the crux of the code is:
Select the sheet and range of cells in the source and copy to the clipboard
Sheets("TheSource").Select
Range("B5:H6").Select
Selection.Copy
Select the sheet and range of cells in the destination and paste from the clipboard
Sheets("TheDestination").Select
Cells(3, 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

Excel copy to clipboard truncating data

I've built a web app which has a paste button for populating a table. The data is originally copied from an Excel spreadsheet and pasted onto my form.
The problem is that I'm only seeing the displayed data, not the underlying values. For example, if the cell's value is 12.223 and the cell's format is only showing 12.2, 12.2 goes on to the clipboard.
Am I missing a trick here? Does anyone know how to pull the full data from the clipboard?
Edit: It appears that Excel makes 25 different formats available on the clipboard, including "XML Spreadsheet" which looks like it contains the actual information I need. However, it appears that only the Text version is available inside the browser. Is there an ActiveX control or something similar that I can use to grab this data?
The only way I can see to do it is to knock up some VBA code that makes use of the Windows Forms Object Library.
I have found a code example that makes use of the clipboard
http://www.dailydoseofexcel.com/archives/2004/12/02/putting-text-into-the-windows-clipboard/
By default this will just copy the visible text of the cell. To get it to output the actual value you'll need to change
cell.Text
to
cell.Value
Although this will probably mess up any dates if you have them.
EDIT:
Actually the dates seem to copy ok
After you've copied the cell, right-click and choose Paste-Special, then choose the Values option under Paste. This will paste the full value.
Given that you can't modify the excel spreadsheet it looks like you are stuck. The issue is that windows clipboard doesn't recognize the browser as being able to accept anything but plain text from the paste. Because of this it pastes it in as a plain text table which basically is just the visual representation of the data that excel presents to you.
If you built an ActiveX component for the page that could except spreadsheet data then the excel paste should work I think. A pure browser javascript implementation won't do what you need though.
Provided that you are happy using an extra sheet to contain the data formatted to copy you could also do the following
Sub CopyPasteSpecialCopy()
'Clear out temp destination
Sheets("CopyPasteSheet").Select
Cells.Select
Selection.ClearContents
'Copy data
Sheets("DataSheet").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
'Paste data
Sheets("CopyPasteSheet").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.Select
Application.CutCopyMode = False
'Put it onto the clipboard
Selection.Copy
End Sub
Although I can't see an option for pasting XML only values.
Again this Macro could be attached to the Ctrl + C key combination.

Resources