Copy sheet to another workbook in VBA does not copy - vba

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.

Related

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 VBA paste from visible cell selection error

I am still new to VBA, but I am trying to paste a copied selection of rows and columns from a different workbook to the current workbook. My issue is that it errors out when I try to paste I assume because the selection is larger than the destination selection. Below part of the code (I know, horrible and with selects):
dim i as long
Workbooks.Open Filename:="C:\Users\...\File1.xls"
Range("A15").Select
For i = 1 To n
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[1],RC[2])"
ActiveCell.Offset(1, 0).Select
Next i
Range("Q15").Select
For i = 1 To n
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-16] ,'[file2.xlsm]DST'!C1:C18,1,0)"
ActiveCell.Offset(1, 0).Select
Next i
ActiveSheet.Range("$A$14:$Q$103").AutoFilter Field:=17, Criteria1:="#N/A"
Range("A1").End(xlDown).SpecialCells(xlCellTypeVisible).Copy
Windows("File2.xlsm").Activate
ThisWorkbook.Worksheets("sheet2").Activate
Range("B1").End(xlDown).Offset(1, -1).Select
'moves the active cell to the data end of column A
Debug.Print "Active cell is " & ActiveCell.Address(False, False)
'it puts the cursor correctly here, but it errors out after in the selection
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Basically, this is part of a code that is adding a new column in a system export file(1), where a unique searching key composed of the second and third column is used later in a VLOOKUP formula to check if there are any missing products and if there are, those will be added in the reporting file(2), at the end of the data that is there. Column B is used as column A has a formula (another CONCATENATE that can't be used for the selection).
How can I make the selection work ?
The following code isn't the solution but it contains it.
Dim WbTarget As Workbook
Dim WsT As Worksheet
Dim Rng As Range
Set WbTarget = Workbooks("File2.xlsm") ' must be open
Set WsT = WbTarget.Worksheets("Sheet2")
Set Rng = WsT.Range("B1").End(xlDown).Offset(1, -1)
With ActiveSheet
.Range("$A$14:$Q$103").AutoFilter Field:=17, Criteria1:="#N/A"
.Range("A1").End(xlDown).SpecialCells(xlCellTypeVisible).Copy Destination:=Rng
End With
Basically, it avoids the PasteSpecial method which would require source and target ranges to be of identical size.
Its big flaw is its reference to ActiveSheet. You have code here to declare and set a workbook, a worksheet and a range. The target workbook, worksheet and range are never selected or activated. I recommend that you treat the source in the same way.
Set Option Explicit at the top of your code sheet. Remember that it is best practice to make all declarations at the beginning of a procedure and, if you like to allow me one more piece of advice, consider writing the results of worksheet functions into your worksheet rather than the worksheet functions.

Copy and Paste (transposed & values) a set range in a different sheet in the next empty row

I have some data in range P1:R13 on a sheet called Training Analysis.
I want to copy and paste these data on a second sheet called Foglio1. I want it to be just values. I need these data to be pasted in a range A2:M4, in other words I want it to be transposed.
I got the following code and it is working. But now, when I get new data I need to paste them under those I already have.
Sub add()
Dim lastrow As Long
lastrow = Sheets("Foglio1").Range("A65536").End(xlUp).Row ' or + 1
Range("P1:R13").Copy Destination:=Sheets("Foglio1").Range("A" & lastrow)
End Sub
It does the empty space but I don't know how to change it to make it transpose the data and give me only values.
Can you help me change it ? If you have new options its fine too.
Cheers
What you need to do when you have a question like this is to record a macro, understand how it works and then clean up the code.
This is what you will get after doing what you need manually and recording it:
Range("P1:R13").Select
Selection.Copy
Sheets("Foglio1").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
After you clean it up a bit and add determining the last row this is what you should get:
Dim lastRow As Long
Sheets("Training Analysis").Range("P1:R13").Copy
lastRow = Sheets("Foglio1").Range("a65536").End(xlUp).Row
Sheets("Foglio1").Range("A" & lastRow + 1).PasteSpecial Paste:=xlPasteValues, Transpose:=True
In this particular case you didn't know that you need to use the PasteSpecial method but this is okay: you don't need to remember the entire Excel object model by heart. You can use the 'record, clean up and modify' method whenever you are in a situation like this.
You could shorten it further and try:
Sub add()
Range("Foglio1!A2:M4").Value2 = Application.WorksheetFunction.transpose(Range("Training Analysis!P1:R13").Value2)
End Sub
This is, of course, adapted to this specific case, so, for further use, you must ensure you update the sheet names and ranges(if they change). You also have to check by yourself that the areas are equivalent (e.g. 15x2 to 5x6 cells). These checks can be added in the procedure, but the code above should do the trick for the moment.
EDIT: I saw your specification a bit too late. :)
Here is the adapted code, which should find the first available row on sheet "Foglio1", column A, and will paste the transposed values onto a 3x13 area. Give it a go.
Sub add2()
With Sheets("Foglio1")
.Cells(.Range("A" & .Rows.Count).End(xlUp).Row + 1, 1).Resize(3, 13).Value2 = _
Application.WorksheetFunction.Transpose(Sheets("Training Analysis").Range("P1:R13").Value2)
End With
End Sub
EDIT 2: updated add2 so that the source range would refer to sheet "Training Analysis" and prevent error# 1004.

VBA Copy Paste is Removing Borders

I'm copying and pasting values from one workbook to another using VBA code. However, when I paste, the borders in the destination worksheet are being deleted. How can I maintain the borders when pasting?
Below is my code:
With wsSource
.Range(.Range("A2"), .Range("C2").End(xlDown)).Copy wsDestination.Range("A3")
End With
I have read that the PasteSpecial method could be of use, but I don't know how to implement it in the above code.
Thanks!
Try this
With wsSource
.Range(.Range("A2"), .Range("C2").End(xlDown)).Copy
wsDestination.Range("A3").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
Whenever in doubt, record a macro ;)

Copy and paste from one workbook to another

I have code in one workbook, this should open another workbook, copy and paste into the workbook with the code. I can select the data but can't paste it.
I have tried many different variations of code getting errors or it doesn't do anything. An example is run in template.xls, which is where I want to paste the data:
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet.Sheets("Data")
.range("A1:H3").Select.copy
selection.copy
End With
I don't know how to use the selection since this will copy from the template, I tried using a full stop before selection.
I can copy the entire sheet from dlsheet into a new workbook, if someone could tell me how to copy it to the template and not a new workbook then this would also do the trick.
dlsheet.Sheets("Data").Copy
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
dlsheet.Sheets("Data").range("A1:H3").copy
ThisWorkbook.ActiveSheet.Paste Destination:=ThisWorkbook.ActiveSheet.Range( "A1:H3")
Try this
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet
.Sheets("Data").Range("A1:H3").Copy
.Sheets("Data").Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With