Copying over drop downs - excel

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.

Related

Formatting not being copied over when using PasteSpecial

I have a Excel sheet which has many conditional formatting rules. When I do a PasteSpecial to another sheet all the formatting disappears.
Why is that?
I need to keep all the rules in unfortunately.
I have tried with xlPasteFormats, xlPasteAll etc but then Excel freezes and hangs.
Sheets("Data").Activate
Range("A1:H12").Select
Selection.Copy
Sheets("Output").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
You could just do a regular copy/paste
or the argument in PasteSpecial:
Paste:=xlPasteAllMergingConditionalFormats
And if you search SO you'll find some information as to why you should be avoiding the use of Select and Activate. Using a regular copy/paste operation, your macro could be rewritten as a one-liner:
Sheets("Data").Range("A1:H12").Copy Sheets("Output").Range("A1")
Thanks for your answers and looking at my questions.
I ended up using
Paste:=xlPasteValues
Paste:=xlPasteFormats
This copied the formatting over.
The reason everything was so slow was because one of the sheets had lots of duplicate Conditional Formatting rules.
I deleted and recreated the sheet with only two Conditional Formatting rules and it was lightning quick.

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

VB Excel PasteSpecial requiring clipboard content?

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.

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