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

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

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

Excel Macro Copy Paste from one sheet to other

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

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

Copy and Paste a Column into a Row in excel using vba

I am trying to write a macro that copies a range of cells (AA4:AA15)(e.g. AA4, AA5,AA6...AA15)
and pastes these values into a new range (C3:N3)(e.g. C3, D3, E3,...N3). The values are found using a formula. I tried using the code seen below, but it only pasted the first value in my copy range, not all of the values. Any help is appreciated.
Range("C3:N3").Value = Range("AA4:AA15").Value
If you did this manually, you would use Paste Special->Transpose. So try:
Sub Macro1()
Range("AA4:AA15"). Select
Selection.Copy
Range("C3").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
End Sub
(Note that I'm only selecting the first cell C3, not the entire range C3:N3)
Excel has a great macro recorder that can help you learn VBA. Simply turn it on and do some stuff and the recorder will create a VBA macro with those exact actions.