How to clear paste range after Excel VBA copy and paste? - excel

I use Application.CutCopyMode = False to clear the Copy range, but the Paste range is still selected. I hate leaving large ranges selected, so usually I'll just do something like "Range("A1").Select".
But that doesn't work if the target for the paste isn't on the selected sheet, so now I have to select the sheet before selecting another cell. I think all that is tacky. There must be a better way to deselect the Paste range, isn't there?

Related

How do I copy a range including hidden cells(by filter)?

I know basically the same questions has previosly been asked and answered on many forums, including stack overflow, but none of the answers satisfy my requirements.
I want to press a button, which will copy a range of around 100 cells. I then want to press a different button, in a totally different workbook (which is in no way connected to the first workbook) and that will paste my copied range.
The issue is that I have a filter in my workbook, which will hide some of the cells in the range. These are not being copied but I need to copy the full range. (The reason I want to copy the full range is because the values need to align when I paste it)
My issue with all other given solutions are:
One solution is to remove the filter when I copy the range. This is not something I want to do as I don't know a way to restore the filter. If this is done before I paste the values, the copied range will "exit?" (it will no longer be copied). And due to the files not being connected, I can't perform any actions with the paste button.
Using a loop to copy the range as a array(Variant), This doesn't work since I can't "transfer" that variant to another workbook which is not connected. Or at least I don't know how to do that.
Is there any other method I can use?
It is a bit of a hack, but because both workbooks have access to the set of custom lists defined for Excel, you could create a custom list with the information needed to identify the source range from the target workbook. The code for the copy button would record the range address, sheet name, and workbook name of the selected range as follows:
Sub copy_range_info()
Application.AddCustomList Array("DeleteMe", Selection.Address, Selection.Parent.Name, Selection.Parent.Parent.Name)
End Sub
On the destination worksheet, the code to paste data would look like this:
Sub paste_range_from_other_workbook()
Dim last_list As Variant
last_list = Application.GetCustomListContents(Application.CustomListCount)
If last_list(1) = "DeleteMe" Then
Workbooks(last_list(4)).Worksheets(last_list(3)).Range(last_list(2)).Copy
ActiveCell.PasteSpecial xlPasteValues
Application.DeleteCustomList Application.CustomListCount
Else
MsgBox "You need to copy a range first using that special button"
End If
End Sub
Because this creates a custom list in Excel that will be permanent, I'm deleting it just before the "else" in the code above. It might be advisable to scan the custom list and delete any lists that begin with "DeleteMe" so if someone does a copy without a corresponding paste, it won't result in more than one custom list of this type.

Copy paste values in Excel multiple selections

I have tried to make multiple selections in an excel sheet where there are formulas in each cell that is highlighted as shown below. I have tried copy/paste these cells, so they only show hardcoded values instead of formulas. Unfortunately, Excel doesn't allow this type of action when you have multiple selections. Is there a way to have multiple selections (as shown below) in Excel and copy only the selected cells and then paste the values with only having to use the paste special option once?
I don't think there is any other way than using VBA in this case.
This code worked for me:
Sub myPasteSpecial()
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value
Next cell
End Sub

Copy formatting only from range with conditional formatting, without the conditions [duplicate]

I know you usually show what you've tried in a question, but this is more of a "Do you have a good routine that does this?" question and I'm hoping you'll be willing to let it slide...
I'm working on a macro that copies cells that are conditionally formatted in a source worksheet and pastes them into an output sheet.
Basically, I'm looking to keep all the formatting, shading, etc, but remove all the conditions (make the current formatting static) in the output sheet.
I've seen some solutions online - ranging from copying it first to a word document and then pasting it back, to looping through the output cells and copying format-element by format-element - and am just looking for a good, efficient way to do this.
Does anyone have one / a good link they'd be willing to share??
(Excel 2010)
THANKS!!!!
Yes it is possible :) What you need to do is change the formatting of the cells that you plan to copy by mimicking the DisplayFormat and then deleting the conditional formatting
Sub Keep_Format()
Dim ws As Worksheet
Dim mySel As Range, aCell As Range
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
'~~> Change this to the relevant range
Set mySel = ws.Range("A1:A10")
For Each aCell In mySel
With aCell
.Font.FontStyle = .DisplayFormat.Font.FontStyle
.Interior.Color = .DisplayFormat.Interior.Color
.Font.Strikethrough = .DisplayFormat.Font.Strikethrough
End With
Next aCell
mySel.FormatConditions.Delete
'
'~~> Now Do the copying
'
'~~> Once you are done, close the sorce worksheet without saving
End Sub
I think I've got it with the Office Clipboard: Copy range, open the Office Clipboard pane (the tiny button in the bottom right corner of the Clipboard section under the Home tab) and paste from there.
Here's a demo: http://www.bookkempt.com/2017/08/remove-conditional-formatting-but-keep.html
I copied my range of cells. Pasted them into Word. Recopied the range in Word and pasted back into excel.
I was trying to do the same, all answers seemed too much of work for me or based on some bug / glitch.
What worked for me is opening ms word copying the whole table from ms excel into ms word then pasting it back to ms excel. The style is there but the conditional formatting doesn't apply anymore.
A very simple method is to filter the list by color. Then FILL with the appropriate cell color you need. then unfilter and remove conditional formatting. The fill will remain.

Excel copy-paste including hidden columns

I'm using a Excel 2016 worksheet that has a couple of columns hidden for UI reasons. I need to be able to filter out data and then copy-paste it to another sheet with hidden columns intact and showing after pasting in the destination (it will contain a longer log of similar transactions, not just one copy-paste).
Adding a pic of the objective - i.e. hoping to have the hidden contents of columns B and C being pasted into the destination spreadsheet. Is this possible at all?
Probably not great form to ask 2 questions in one post, however are there alternatives to performing filtering and copy-paste function to another spreadsheet manually? I.e.:
run manual filter to clear blanks in Quantity field;
make a selection
do manual Ctrl+C - Ctrl+V function
Is there a way to make it easier? Unfortunately no VBA or macro experience as of yet.
Edit - Completely misunderstood the question!
You want to include hidden cells when you copy - that's standard behavior for hidden cells but not for filtered columns. If you want to avoid VBA abd you're dealing with small contiguous ranges then a simple formula may be the easiest solution.
Using your example, I will arbitrarily name the source worksheet "Sheet1" and the destination "Sheet2". In Sheet2, click in cell A2 and type this into the formula bar: =Sheet1!A3 Now click the bottom right corner of cell A2 and drag it to the right through D2 then down to D7.
With the range highlighted, press ctrl C to copy, then right click to paste special values.
You're done!
Here's a VBA solution:
Sub copyrng()
Dim srcrng As Range
Dim tmprng As Range
Dim dstrng As Range
Dim srcws As Worksheet
Dim dstws As Worksheet
Set srcrng = Application.InputBox("Area to copy", "Source", Type:=8)
Set srcws = srcrng.Parent
Set tmprng = Application.InputBox("Top Left Corner of Destination", "Destination", Type:=8)
Set dstws = tmprng.Parent
Set dstrng = dstws.Range(tmprng.Address, tmprng.Parent.Cells(tmprng.Row + srcrng.Rows.Count - 1, tmprng.Column + srcrng.Columns.Count - 1))
dstrng = srcrng.Value
End Sub
First answer (answered wrong question)
You can copy visible cells using "Go To..."
Highlight the range you want to copy, press Ctrl G, click "Special...", select "Visible Cells Only", and then press Ctrl C to copy.
Now all hidden cells will be left behind when you paste.
No, you cannot do this with regular Excel features since Excel cannot know which columns/cells to skip when one of the column have blank values, this is something has to be decided and done by a human.
Maybe this is a good time to enter the world of Macros, since you do not need a custom code but can use the recorded macro without any further manipulation. This Excel feature is for inexperienced users just like you.
View / Macros / Record Macro
Name your macro
Do what you need to, keeping in mind that Excel is recording your every move by converting them into VBA codes in the background. For your case, do the following:
Filter the blanks using filter combo-box
Select the range by using CTRL-G / Special / Current Region (do not select the cells by mouse or with your keyboard, your code should be generic should not contain manual ranges since you do not want to do any coding)
CTRL-C to copy
If "to-be-pasted" cell is not fixed for all your cases, then you should stop recording your macro here. If pasting cell is fixed then Paste the contents while the macro is recording.
After the macro is recorded, assign a shortcut to your new Macro using:
Macros / View Macros / Options menu
Voila! Now you are able to do exactly what you have done when recording your macro by using that keyboard shortcut. If you did not paste the content when recording then you s/b using your macro short cut and go to the cell you want to paste and press CTRL-V.
When you feel confident enough, try the Edit menu in the Macros and see what code you have in hand, maybe make some small changes etc. I saw many people who are not familiar with basic coding at the beginning but somehow started writing their own codes after seeing this feature in Excel. Good Luck!
ProfoundlyOblivious code is pretty cool but the
dstws = activesheet
will always be the source since the activesheet passes back straight after the inputbox.
I tried changing it to
Set dstws = tmprng.Parent
but for some reason this then breaks the
Set dstrng = dstws.Range..
I get a Run time error 1004 Method range of object _Worksheet failed?!?!
If I could fix that this solution would work for you with any destination, even other files.
The alternative is to use vba to un-filter the data, then do a copy, then put same filter(s) back on. Once that is done you can go anywhere and paste what is now on the clipboard.

Copy and paste VBA

I am working on a project which needs a lot of copy and paste cells between work sheets in excel which are not in order .I could barely program VBA . I am thinking that if i can write a program (click or right click to copy to clip board and double click to paste in different work sheet) that could make my work faster .Please suggest me.
you could use an excel formula to copy from one sheet to another
follow the instructions on this site
http://ccm.net/faq/9795-transfer-data-between-excel-spreadsheets
it basically says to
in the target cell type +
then select the source worksheet
click on the source cell
then hit enter
these steps will make a copy of, for example, work sheet 1, cell A1 into work sheet 2 cell A1
the formula is as follows =+Sheet1!A1
I hope this is of some help to you
You have a few options here, but let me first explain one thing to you.
When you are copying and pasting cells, you are actually using the Microsoft clipboard which is rather memory intensive process, and while this is the go to default option you could just as easily just assign value to value which doesn't use the clip board.
No frills option:
Sub Copy_and_Paste()
Sheets("Your Sheet Name").Range("Your Range").Copy
Sheets("Your Destination Sheet Name").Range("Your Destination Range").Paste
End sub
Example No frills
Sub Copy_and_Paste()
Sheets("Sheet 1").Range("A1:A10").Copy
Sheets("Sheet 2").Range("B1:B10").Paste
' You don't have to paste them in the exact same range.
' Just make sure they are the same size.
End sub
Values option
Sub Value_to_Value()
Sheets("Your Destination Sheet Name").Range("Your Destination Range").value = Sheets("Your Sheet Name").Range("Your Range").value
End sub
Example Values
Sub Value_to_Value
Sheets("Sheet 2").Range("B1:B10").value = Sheets("Sheet 1").Range("A1:A10").value
End sub
Experiment with both to come up with the version you prefer.

Resources