Excel Vba copy and paste - excel

While copying data from one Excel sheet to another workbook I have to paste data with the same format because each cell has interior color. I will also delete the data from the original sheet which I copied from. Is it possible to keep the source formatting?
Workbooks.Add
Set Outbook1 = ActiveWorkbook
OutBook.Activate
OutBook.Sheets("Sheet6").Select
Lines = ActiveSheet.Range("A65536").End(xlUp).Row
Range("A1:N" & Lines).Select
Selection.Copy
Outbook1.Activate
Outbook1.Sheets("Sheet1").Range("A1").PasteSpecial (xlPasteAll)

You can keep the source formatting (as long as it's not conditional formatting), using the PasteSpecial xlPasteAll command.
Recommendation: It's always better if you stay from Activate , Select and Selection, instead use referenced Workbooks and Sheets.
(for more details go to How to avoid using Select in Excel VBA macros )
Code
Option Explicit
Sub CopyBetweenSheets()
Dim Outbook1 As Workbook
Dim OutBook As Workbook
Dim Lines As Long
Set OutBook = ThisWorkbook
Set Outbook1 = Workbooks.Add
With OutBook
With .Sheets("Sheet6")
' find last row in Column A in "Sheet6"
Lines = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A1:N" & Lines).Copy
End With
End With
' paste to "A1" in "Sheet1" in the new workbook
Outbook1.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteAll
End Sub

Related

Copy row from one sheet to recent sheet

I want to simply copy the first row "test" into all following sheets (Sheetxx1, Sheetx23, Sheet231, etc. ) ... (like 100 following sheets with different names).
So I tried this by recording a macro (with relative reference) and then went on the sheet, where I want to have it pasted (like Sheetx231) and then did run the macro. But what it did is it pasted again row "test" into Sheetxx23.
How can I make the macro paste the row test of Sheetxx1 into the recent sheet (I am in and run the macro)?
Sub Macro1()
Rows("1:1").Select
Selection.Copy
Sheets("Sheetxx23").Select
ActiveCell.Rows("1:1").EntireRow.Select
ActiveSheet.Paste
End Sub
You need to loop throuh all worksheets and copy/paste for each worksheet.
Option Explicit
Public Sub CopyFirstRowIntoAllWorksheets()
Dim SourceWs As Worksheet
Set SourceWs = ActiveSheet
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets 'loop throuh all sheets
If Not ws.Name = SourceWs.Name Then 'makes sure source and destination is not the same sheet
SourceWs.Rows(1).Copy Destination:=ws.Rows(1) 'copy first row
End If
Next ws
Application.CutCopyMode = False
End Sub
You might benefit from reading
How to avoid using Select in Excel VBA.
Edit according comment:
If you need only the worksheets right of the active worksheet to be pasted, replace
If Not ws.Name = SourceWs.Name Then
with
If ws.Index > SourceWs.Index Then

How to copy the entire sheet to newly created sheet referencing the sheet's name in Excel?

I wanted to create a sheet and rename it from referencing a cell in another sheet. This worked so far.
However, I'd like to copy and paste everything as values but maintaining the format from the old sheet to this newly created sheet. I don't know how to reference this just newly renamed sheet.
Code as follows:
Private Sub CreateSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:=Worksheets("OldSheet"))
'the new sheet has the name from B3 of the old sheet.
ws.Name = Sheets("OldSheet").Range("B3")
'this copy paste part doesn't work, it gives an error.
Sheets("OldSheet").Copy
Sheets("ws").PasteSpecial Paste:=xlPasteValues
End Sub
Here is a simple way to do it. Create a copy of the existing sheet and then use UsedRange.Value = UsedRange.Value
With ThisWorkbook
.Sheets("OldSheet").Copy After:=.Sheets(.Sheets.Count)
With .Sheets(.Sheets.Count)
.Name = ThisWorkbook.Sheets("OldSheet").Range("B3").Value
.UsedRange.Value = .UsedRange.Value
End With
End With
You can use the below code to copy the data from one sheet to another. Found few issues with the code you were using.
You have to make the new sheet visible. (optional though).
Select the range you want to copy from old to new sheet. (Below code will copy range A1:C5 and paste it from A1 range of new sheet.
Private Sub CreateSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
Worksheets("OldSheet"))
'the new sheet has the name from B3 of the old sheet.
ws.Name = Sheets("OldSheet").Range("B3")
ws.Visible = xlSheetVisible
'this copy paste part doesn't work, it gives an error.
ThisWorkbook.Sheets("OldSheet").Range("A1:C5").Copy
ThisWorkbook.Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteValues
ThisWorkbook.Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteFormats
End Sub
Update:
As suggested in comments you can use UsedRange to copy.
You can replace below line of code in original code above.
uRange = ThisWorkbook.Sheets("OldSheet").UsedRange.Address
ThisWorkbook.Sheets("OldSheet").Range(uRange).Copy
ThisWorkbook.Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteValues
ThisWorkbook.Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteFormats

How to copy a worksheet to a new worksheet but as values only?

I copy a worksheet "Costing Sheet" to a new worksheet.
I use this code:
Sub CopyPasteSheetAsValues()
Worksheets("Costing Sheet").Copy After:=Worksheets("Costing Sheet")
Application.CutCopyMode = False
End Sub
There are formulas in the copied sheet that I do not need in the new worksheet. I would like to copy the worksheet into a new sheet but paste as 'values only'.
I also have the code below that can copy and paste the worksheet but it overrides the current sheet. Instead of a new worksheet with values only, it's the same worksheet as values.
Sub CopyPasteSheet()
'Copy and Paste Summary Sheet as Values
Sheets("Costing Sheet").Cells.Copy Before:=Sheets("Comparison Job")
Sheets("Costing Sheet").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
I tried to combine the codes but was unsuccessful.
You can exploit the fact that a sheet becomes the active one when it is copied from another sheet
Sub CopyPasteSheetAsValues()
Worksheets("Costing Sheet").Copy After:=Worksheets("Costing Sheet")
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
End Sub
Doing a copy with no destination will create a new workbook with the copied worksheet. Assigning the value of the range to the value of the range will get rid of the formulas.
Dim myNewWB as Workbook
Worksheets("Costing Sheet").Copy
Set myNewWB = ActiveWorkbook
With myNewWB.Worksheets(1)
.Range(.Cells(1,1),.Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Count)).Value= _
.Range(.Cells(1,1),.Cells(.UsedRange.Rows.Count,.UsedRange.Columns.Count)).Value
End With

Copy a hidden sheet (which includes shapes that have macros assigned to them) to another sheet and hide the sheet back

I have a sheet with seven rectangles, each rectangle has a macro assigned to it and I have beautified it like a template.
There are many such sheets and I want them all hidden.
Now, I want to unhide a sheet ,say sheet1, copy all its contents including the shapes and respective macros in the exact same format, paste it in sheet 3 and hide sheet 1 again.
I have tried this with the following code but somehow the objects and the format is not getting copied.
Sub abcd()
Dim ws As Worksheet
Set ws = Sheets("Sheet2")
ws.Visible = xlSheetVisible
ws.Select
Selection.Copy
Dim rs As Worksheet
Set rs = Sheets("Sheet4")
rs.Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
How can I achieve this?
You should copy the whole sheet, not just the selection:
'// get the sheet to be copied
Dim ws As Worksheet
Set ws = Sheet1
'// copy it and position before, say sheet5
ws.Copy Before:=Sheet5
'// get a reference to the new sheet
Dim WSNew as worksheet
Set WSNew = Sheets(Sheet5.Index - 1)
'// Give the sheet a new name and make it visible.
WSNew.Name = "New name"
WSNew.Visible = xlSheetVisible
Note that I am using the internal sheet programming name, not the display name- that is more reliable as the user cannot change the internal name, although you can change it in the properties tab for the sheet.
Also, there is no need to make the template sheet visible, just the copied one.

Setting value using filtered data

I'm trying to open a sheet (Archive) from my inventory sheet, filter the data in the second sheet and then copy the filtered data to a sheet on the inventory. Everything is working except that the filtered data only copies the data from rows in the first contiguous range. My code is as follows
Dim LastRow As Long
Dim nOoFrOWS As Long
Dim oSht As Worksheet
Workbooks.Open ("C:\Inventory\Archive.xlsm") '<- at opening a workbook it becomes the active one
Set oSht = ActiveWorkbook.Worksheets("Archive") '<-- set the destination worksheet in the activeworkbook
With ActiveSheet
.ListObjects("Archive").Range.AutoFilter Field:=12, Criteria1:=mOrder
nOoFrOWS = .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1 '# of rows in Inventory
End With
Unload Me
ThisWorkbook.Sheets("RAM").Range("A2:K" & nOoFrOWS).Value = oSht.Range("Archive[[QTY]:[RTK]]").SpecialCells(xlCellTypeVisible).Cells.Value
oSht.Parent.Close False
What am I doing wrong?
edit: I don't know if it is pertinent, but the range in the archive (from which I am copying) is not the entire table. I have more rows, but These are all I need for this application.
Also, is there a way to do this without the clipboard by using .value or am I stuck with using the copy paste method?
As your working with a table you can copy the visible cells in the databodyrange.
No need to activate or select anything - just work with the referenced files & sheets.
Sub Test()
Dim wrkBk As Workbook
Dim mOrder As Long
mOrder = 5
'You can reference the workbook without it being active.
Set wrkBk = Workbooks.Open("C:\Inventory\Archive.xlsm")
With wrkBk.Worksheets("Archive").ListObjects("Archive")
.Range.AutoFilter Field:=12, Criteria1:=mOrder
'Copy the DataBodyRange (Range would include the headers).
.DataBodyRange.Resize(, 11).SpecialCells(xlVisible).Copy Destination:=ThisWorkbook.Worksheets("RAM").Range("A2")
End With
End Sub

Resources