Excel Macro Copy Paste from one sheet to other - vba

I have a macro which does few calculation in another workbook and creates a new sheet where it writes the final data.
In the last step, the values from the new sheet should be copied to the current workbook where the Macro is written.
I have written the following line to do the paste activity. But my problem is, each time the data gets pasted in different places in the workbook. Is there a way for me to paste values starting from particular column
ThisWorkbook.Activate
Sheets(1).Select
ActiveSheet.Paste

Of course, if its a static range:
Range("B3:D7").Select 'Or the range you need
Selection.Copy ' Copy that selection
Sheets("yoursheet").Select 'Select the sheet or workbook where you will paste info
ActiveSheet.Paste ' Paste data
Tell me how it goes.
To select the range in destination Sheet use this order :
Sheets("yoursheet").Select
Range("A1").PasteSpecial Paste:=xlPasteValues

Related

Macro for copying certain cells between excel worksheets

I would like to make a macro which will copy certain cells values marked by user in one sheet to another,but into different cells.
In one worksheet we have data in cells from A1 to D1,my goal is to paste them into second worksheet but to another cells (A2,A4,A6,A8 in my case)
And also when somebody copies for example more than four cells it will also paste them right next (B2,B4,B6,B8 and so on..)
I've managed something like this but it does not work
Sub sbCopyRangeToAnotherSheet()
'Copy the data
Sheets("Arkusz2").Range("A2:D2").Copy
Sheets("Arkusz1").Activate
'Select the target range
Range("A2", "A5", "A8", "A11").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
The correct syntax is
Sub sbCopyRangeToAnotherSheet()
Sheets("Arkusz2").Range("A2:D2").Copy
Sheets("Arkusz1").Range("A2,A5,A8,A11").PasteSpecial
Application.CutCopyMode = False
End Sub
or you can even do it in one line:
Sub sbCopyRangeToAnotherSheet()
Sheets("Arkusz2").Range("A2:D2").Copy Sheets("Arkusz1").Range("A2,A5,A8,A11")
End Sub
And I recommend to read: How to avoid using Select in Excel VBA.

VBA code issue for copying and pasting same columns from multiple sheets in excel workbook

Having a problem with the code.
The excel workbook has 4 sheets. The workbook is regularly updated.
-Sheet1 is where I want the data pasted to.
-Sheets2-4 have the data that I am trying to get.
-The range "A2:B2" is where the data is at in Sheets2-4. Data needs to be pasted in the same range in Sheet1. No data pasted.
The below code results in A2:B2 only selecting in Sheet2-4 and the same range being selected and copied in Sheet1.
Any help would be appreciated.
Sub test()
Worksheets.Select
Range("A2:B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Your code is copy/pasting from sheet1 to sheet1.
The first line of your code selects all sheets.
Then it selects the current range of first sheet, and applies the same selection to all sheets.
When you copy the selection on multiple sheets, only first sheet gets copied.
Then you paste the sheet1, to sheet1.
You cant copy from multiple sheets in one command.
If you could, then pasting values from 3 sheets into the same destination range in 1 sheet is also not possible.
Your questions states that you only need range A2:B2, but your code selects the used range below the code which is unnecessary if you only need range A2:B2.
Here is some code to copy/paste from a single sheet:
Sub CopyPaste()
Worksheets("Sheet2").Range("A2:B2").Copy Destination:=Worksheets("Sheet1").Range("A2:B2")
End Sub
if you would prefer copying the activesheet, then remove the sheet reference from the above code:
Sub CopyPaste()
Range("A2:B2").Copy Destination:=Worksheets("Sheet1").Range("A2:B2")
End Sub

How do I implement the VBA module to copy filtered data

I need to implement a VBA Macro that copies data from one excel worksheet and puts certain column into another one.
Sub sbCopyRangeToAnotherSheet()
'Method 1
Sheets("Sheet1").Range("A1:B10").Copy Destination:=Sheets("Sheet2").Range("E1")
'Method 2
'Copy the data
Sheets("Sheet1").Range("A1:B10").Copy
'Activate the destination worksheet
Sheets("Sheet2").Activate
'Select the target range
Range("E1").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
This wont work for new sheets named for example Sheet 3 neither will it run effectively for filtered data. I need a code that can copy the filtered data and paste certain column into another worksheet.
This simplest way is copyas commend from oryginal file "with code" and delete this, what you do not want to share.
Another one relies on building string with code lines using VBComponents
There you can check the code: http://vbatools.pl/tworzenie-linii-kodu-makrem-z-dodatku/
After that you file should to save as xlsm ora xls (old school) to save makros.

How to paste value of a sheet using VBA?

I have always used following to make a sheet to contain only values:
Sheets("NameOfTheTab").Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:= xlPasteValues
But this is not safe, if someone/event change the selection, the program runs into random behaviour. How can can I get rid of this pattern?
Well, for one thing, you can avoid selecting the cells, just directly copy them:
Sheets("NameOfTheTab").Activate
Cells.Copy
Cells(1,1).PasteSpecial Paste:= xlPasteValues
Although this might not be the fastest way, depending on your sheet/actual problem.
You should always avoid using select method, beacuse it is not reliable. The sub below is a sample to copy data from a worksheet and paste only values to another one. This sample sub assumes you are running this macro from your target workbook if not change ThisWorkbook to your target workbook.
Sub copy_paste_only_values()
'will copy all cells in your tab that contain data
ThisWorkbook.Worksheets("NameOfTheTab").Cells.Copy
'will paste only values to your target worksheet
ThisWorkbook.Worksheets("NameOfTheTargetTab").Range("A1")._
PasteSpecial Paste:=xlPasteValues
'empty the clipboard
Application.CutCopyMode = False
End Sub

Excel VBA: Copy rows from another workbook, but just the function results

I want to copy 10 rows from a workbook into a new workbook. The copy & paste is fine, but in the copied cells are some formulas that I need to replace with the results.
Is there a magical way to copy only the displayed values from the cells?
This is how I do it now:
Rows("2:11").Select
Selection.Copy
myWorkbook.Sheets(1).Activate
ActiveSheet.Rows("1:10").Select
ActiveSheet.Paste
Rows("2:11").Copy
myWorkbook.WorkSheets(1).Rows("1:10").PasteSpecial xlPasteValues
Application.CutCopyMode = False