Copy and paste VBA - excel

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.

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.

Read One Cell and Copy to Another Only Upon Opening Spreadsheet

I need to copy the data in cell c3 to cell i11 when I open my spreadsheet. I do not want i11 to change after this action until I reopen the spreadsheet.
You can set the code in the ThisWorkbook part of your projects. Select Workbook and Open or use the following code.
The working formula is the value you see me do below, but this can be achieved a number of way such as using Cells(3,3).Value. Additionally the way the sheet is referenced can vary. I put both the Activesheet reference and a explicit sheet reference, depending how your sheet is structured you may have to use one over the other, but choose one below and see how your sheet handles it and if adjustments are needed.
Private Sub Workbook_Open()
'Use one of these depending on how your sheet opens
ActiveSheet.Range("I11").Value = ActiveSheet.Range("C3").Value
Worksheet("YourWorksheetHere").Range("I11").Value = Worksheet("YourWorksheetHere").Range("C3").Value
End Sub

How To: Let users paste cells from another excel instance (Ctrl+V,...) but paste only values

EDITED WITH PROGRESS
Dear Stackoverflow community,
I am working on a big excel file that does some calculations for me and my colleagues. Because the calculation data is a lot and is entered in Ranges (like "A1:H8"), not single cells (like "A1","C1",...), I want the users to be able to copy data from the same or another excel instance to my file.
The problem (edited):
The problem is, that just pasting cells formats the target cells (even if they are protected against formatting) and this has to be avoided. I searched through a lot of online discussions and finally made my own code, that allows me to copy and paste between two excel files in the same excel instance. Sadly, it does not work, if I copied the cells from another instance.
The code:
This is the code I use in "ThisWorkbook":
Sub PasteValuesOnly()
'if cells are pasted in named worksheets, only values are pasted
'is linked to Ctrl+V in options of macro menu
On Error GoTo err_handler
Dim Target As Range
Set Target = Selection
If Target.Parent.Name <> "Table1" Then
Selection.PasteSpecial
Else
Selection.PasteSpecial Paste:=xlPasteValues
End If
err_handler:
Exit Sub
End Sub
The system:
Windows 7
Excel 2010
What I tried besides my code (new progress):
As mentioned in the comments, I know Siddharth Rout's solution for only letting the users paste values, but I can't get it to work (not even in a fresh file when copying and pasting inside one Excel instance). I tried it for the whole workbook and for one single sheet.
What would help (edited):
It would be very helpful, if you could tell me how to optimize my code, so it works for two instances as well. If you know what is to do when I have an error with UndoList = Application.CommandBars(“Standard”).Controls(“&Undo”).List(1) in Siddharth Rout's solution with Excel 2010, this would be helpful, too.
Otherwise I would like every solution, that let's my users paste like they ever do, but prevent them from formatting the cells while pasting.
Thank you in advance
RaspiManu
After long hours of searching the internet, I found the solution of Donna Landy (Bella_Donna) in the microsoft forum. Her code is simple and works for CTRL+C / CTRL+V, Copy and Paste over Right Click Menu, Drag'n'Drop and even with two excel instances.
Because it starts on every single cell change and goes back to the cell or range that was changed, I slightly optimized it for my needs. Now, users that enter a list manually won't have to press "Enter" two times every time, they want to get to the next line below.
Assuming, the standard user will normally copy and paste, if there is a range of data, he or she does not want to retype, I changed the code, so the module sub only gets activated, if more than one cell has been changed (see below).
The solution:
In every worksheet, that has to be protected against formatting (modified):
Private Sub Worksheet_Change(ByVal Target As Range)
'activates format protection when changing a range
If Target.Cells.Count > 1 Then 'If more than one cell has been changed...
Call Worksheet_Change_Protected(Target) '...activating protection
End If
End Sub
In a module (unmodified):
Sub Worksheet_Change_Protected(ByVal Target As Range)
'Prevents user blithely obliterating in-cell formatting by undoing their paste and pasting the value
'Donna Landy 26.11.2018
'May be freely copied - hat tip appreciated :)
Dim SavedVal As Variant
On Error GoTo ErrHan
'Save the pasted value for later
SavedVal = Target.Value
'Switch off events to prevent infinite loop
Application.EnableEvents = False
'Undo the user's paste
Application.Undo
'Set target value
Target.Value = SavedVal
ErrExit:
'Remember to re-enable events
Application.EnableEvents = True
Exit Sub
ErrHan:
Resume ErrExit
End Sub
Thank you very much, Donna Landy!

macro to use active cell and copy multiple cells to another sheet in different cells

I am trying to copy info from one sheet into another using a macro. I'd like to be able to select a cell in Sheet A (eg A10), and using that as a reference copy cells in the same row, eg c10, d10 and g10, and paste that info into static cells in Sheet B, eg $A$6, $A$7, and $A$8.
Have looked through the message boards but haven't found anything that uses the active cell, and multiple cells.
Thanks in advance!
Adrian
The task can be completed using simple statements like the following sample:
Worksheets("Sheet A").Range("A10").Copy destination:=Worksheets("Sheet B").Range("$A$6:$A$8")
In case you want be able to select a cell in Worksheets("Sheet A") and copy it to the destination Range (as shown above), then you may create a Sub like the following one:
Sub PasteSelection()
Selection.Copy Destination:=Worksheets("Sheet B").Range("$A$6:$A$8")
End Sub
and call it using, for example, ALT+F8 shortcut to run the Excel VBA macro.
Hope this may help.

Linking cells to each other; having cells update linked cells and vice versa

I have two ideas that could lead to more or less the same result.
I am trying to have similar cells or tables update themselves to whatever the most recent entry was in the linked system. For example, cell A1 is linked to cell B2 (in this system that I am trying to create). I would enter something like "1" or "Text" or whatever in A1 and the system would update cell B2 to whatever I entered in cell A1. However the opposite must work in the same manner. If I changed whatever in cell B2 to, say, "5", cell A1 would also display "5". Also, note that I have Excel 2013.
I need this to work with a cell or a table. So, getting to the possible solutions...
A subroutine in VBA that automatically updates all the linked cells or tables.
Some sort of mechanic unknown to me that uses VBA or another Excel aspect to do this, like a function or tool.
In your answer or solution, please be mindful to my inexperience with VBA. Thanks in advance.
You can try a worksheet change function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address then
ActiveSheet.Range("B1").Value = ActiveSheet.Range("A1").Value
ElseIf Target.Address = Range("B1").Address then
ActiveSheet.Range("A1").Value = ActiveSheet.Range("B1").Value
End If
End Sub
Although this seems like it might create an infinite loop (the update from the change causes another change) it DOES work for me in Excel 2010..
There are other Worksheet functions you can try as well (e.g. Worksheet_SelectionChange)
This macro needs to be placed/entered as a WORKSHEET macro on the sheet where you desire to use it..it will not work in a Module.
To install:
1) Save your workbook as a macro-enabled file.
2) Close Excel, reopen file, and enable macro security
3) Type Alt-F11
4) In the project explorer view on the left, look for your sheet name. Double click it
5) In the code entry area on right (big window) paste the example code above
6) Return to your worksheet and try it.

Resources