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

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

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.

Copy sheet to another workbook in VBA does not copy

I am trying to copy a specific sheet from one workbook to another but it seems that it does not copy the content of the sheet and returns an empty one.
Second problem is that when I copy the sheet I use Sheet.Count to create another sheet in my workbook where I paste the new sheet but it is not working properly as it does not create a new one as and instead takes my last one and renames it and then deletes it.
What could it be wrong and how do I add error handling code?
Sheets(filesheet).COPY After:=ThisWorkbook.Sheets(Sheets.Count)
'Application.AskToUpdateLinks = False
wb.Close savechanges:=False
'naming of the copied sheet
ActiveWorkbook.Sheets(Sheets.Count).Name = "Imported data"
'copy the material as values to sourcing template sheet
Sheets("Imported data").Cells.COPY
Sheets("Sourcing template").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'delete the imported copied sheet (named ranges etc, to avoid file size growing too much)
Sheets("Imported data").Delete
There are tons of problems here but ill try to explain some....
Dont copy and paste. You thisCell.Value = ThatCell.Value instead.
Referencing Active(whatever) is going to eventually lead you to bad results. It may seem like a pain point to type more, but explicitly spelling it out what workbook/sheet/whatever you are intending to reference will solve alot of frustration in the future.
Here is most likely how i'd approach this like this. This is easily modified to do all sheets in a work book.
Dim sheetCopy as variant
dim sheet as Worksheet
dim rowC as long
dim colC as long
set sheet = ThisWorkBook.Sheets("Imported Data")
rowC = sheet.UsedRange.Rows.Count
colC = sheet.UsedRange.Columns.Count
sheetCopy = sheet.UsedRange
ThisWorkBook.Sheets("Sourcing template").Range(ThisWorkBook.Sheets("Sourcing template").Cells(1,1), ThisWorkBook.Sheets("Sourcing template").Cells(rowC,colC).Value2 = sheetCopy
sheet.Delete
To be frank, I don't fully understand your question.
But base on my understanding, this code works for me. The filesheet in Sheets(filesheet) doesn't make sense for me, so I've changed it to "Sheet1". You can change it ot your exact sheet name.
Sub test()
Sheets("Sheet1").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
ActiveWorkbook.Sheets(Sheets.Count).Name = "Imported Data"
Sheets("Imported data").Cells.Copy
Sheets("Sourcing template").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Imported data").Delete
End Sub
The code copy all the content of Sheet1 to new sheet and change the new sheet name as "Imported data".
Then, copy all cells in "Imported data" and paste only the values to "Sourcing template". Thus, only values will be shown in "Sourcing template" (any chart or table will be ignored).
If you wish to paste all the content including formatting, you should use Activesheets.Paste instead.

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.

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