I'm trying to copy and paste the values form another workbook with the specific sheet named Sheet1. But when I run this code:
Dim wb as Workbook
Dim conso as Worksheet
set conso = wb.Worksheets("Sheet1")
With conso
.Rows(1).EntireRow.Copy
End With
With ActiveSheet
'.Paste
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
End With
I got an error saying:
PasteSpecial method of Worksheet class failed.
I'm trying to copy all the entire row of the Sheet1 sheet to another workbook. Any help?
Try indicating where you need to paste the cells you copied:
With ActiveSheet.Rows(1)
'.Paste
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
End With
Related
I'm just getting familiar with VBA and my code
For k = 3 To ThisWorkbook.Sheets.Count
ThisWorkbook.Sheets(k).Activate
ActiveSheet.Cells(11, 2).Select
lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("A5:" & "A" & CStr(lLastRow)).Copy
' ThisWorkbook.Sheets(1).Cells("B" & CStr(lFirstRow) & ":B" & CStr(lLastRow)).Select
ThisWorkbook.Sheets(1).Activate
ActiveSheet.Cells(lFirstRow, 2).Select
Selection.Paste
'Selection.PasteSpecial Paste:=xlPasteValues
'Selection.PasteSpecial Paste:=xlPasteFormats
lFirstRow = lFirstRow + lLastRow
Next k
makes "Run-time error 438. Object doesn't support this porperty or method" to appear when the line "Selection.Paste" goes. What's the problem?:(
I've tried to use paste special, to activate sheet and to select cell (not range), to use Cstr, but nothing changed
Try Selection.PasteSpecial xlPasteAll
Paste by itself works on several objects, most notably Worksheet but not on a Range object which is what your Selection is.
To paste to a Range you really have to use the PasteSpecial method with its' available arguements such as xlPasteAll; xlPasteValues; xlPasteFormulas; xlPasteFormats and others which you can see by pressing F1 while the cursor is within PasteSpecial in the VBE.
Replace these two lines in your code
ActiveSheet.Cells(lFirstRow, 2).Select
Selection.Paste
by
Cells(lFirstRow, 2).Select
Activesheet.paste
your code will work flawlessly
Important note for working with paste and pastespecial in vba
Copy any range from anywhere then
Paste Special method (Sheets.Cells/Range.PasteSpecial)
Sheets ("Daily Shortage").Activate
Sheets ("Daily Shortage").Cells (m, 1). PasteSpecial Paste: = xlPasteValues
One Example –
Will throw error
Sheets ("June"). Range ("A10").Select
ActiveSheet.PasteSpecial Paste: = xlPasteValues
This will work flawlessly
Sheets ("June"). Range ("A10").PasteSpecial Paste: = xlPasteValues
Paste method (ActiveSheet.Paste)
Sheets ("June"). Range ("A10").Select
ActiveSheet.Paste
I am still new with VBA but decided using VBA to streamline a Monthly report I am working on for our payroll would be the most efficient way, I wish to export a range I3:U2270 into a new sheet named from a cell in the summary sheet. Everything works fine except formatting.
If I use xlPasteAll the formatting is perfect, but it shows values as formulas.
If I use:
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
The data and cell widths are perfect, but because some data is in Tables, it doesn't bring the formatting across.
Any ideas would be appreciated!
Sub MonthlySummaryExport()
Dim NewSheetName As String
Dim Newsheet As Object
On Error Resume Next
NewSheetName = Worksheets("Monthly Summary").Range("T1")
If NewSheetName = "" Then Exit Sub
Set Newsheet = Sheets(NewSheetName)
If Not Newsheet Is Nothing Then
MsgBox "Sheet cannot be created as there is already a worksheet with the same name" & " " & (NewSheetName)
Exit Sub
End If
Sheets.Add(, Sheets(Sheets.Count)).Name = NewSheetName
'Copy and PasteSpecial a between worksheets
Worksheets("Monthly Summary").Range("I3:U2270").Copy
With Worksheets(NewSheetName).Range("A1")
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
End With
'Disable marching ants around copied range
Application.CutCopyMode = False
End Sub
Copy everything including formulas, then change formulas to values in the copy
'Copy and PasteSpecial a between worksheets
dim dest as range
set dest = Worksheets(NewSheetName).Range("A1")
Worksheets("Monthly Summary").Range("I3:U2270").Copy dest
dest.currentregion.copy
dest.pastespecial xlPasteValues
Sub MakeTables()
Dim wbTarget As Object
Set wb = Workbooks.Open("C:\Users\A9900899\Desktop\Desmond\VBAProject\GenerateTablesFormulas.xlsx")
Set wbTarget = Workbooks.Open("C:\Users\A9900899\Desktop\Desmond\VBAProject\USDReport.xlsx")
With wb.Sheets("Sheet1").UsedRange
.Copy
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82:X97")
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With
End Sub
Can someone tell me what mistake I made here. It gives me the error that range class failed. Thank you
if you want to keep the copied range size, then:
With wb.Sheets("Sheet1").UsedRange
.Copy
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82") '.Resize(.Rows.Count, .Columns.Count) '<--| you can omit the 'Resize' part but it can be useful to make it clear (code can be read in years to come) you want to stick to copied range size
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With
Application.CutCopyMode = False '<--| empty clipboard
otherwise if you want to force the copied range size to the to-be-pasted one, then:
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82:X97") '<--| reference range you want to paste values to and keep sizes of
wb.Sheets("Sheet1").UsedRange.Resize(.Rows.Count, .Columns.Count).Copy '<--| copy the source sheet range with same sizes as referenced range
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
Application.CutCopyMode = False '<--| empty clipboard
The code below works but isn't fast and I'm sure there are ways it could simplified. I'm not a coder -- I just compiled a few samples that I found. I really don't understand the range/end/offset pieces for the pasting aspect. Here's what I'm trying to do:
1. Print the first three worksheets in the workbook
2. Create three new worksheets at the end of the workbook
3. copy and paste values, formats, and column widths to the three new worksheets from the first three.
Thanks for any help you can provide!
Option Explicit
Option Base 1
Sub Print_copy_Current_Workbook()
'Prints the current active workbook in Excel
Sheets("Draw").PrintOut
Sheets("Calculations").PrintOut
Sheets("AIN").PrintOut
Application.ScreenUpdating = False
Dim Tabs As Variant
Dim I As Byte
Tabs = Array("Draw Final", "AIN Final", "Calculations Final")
For I = LBound(Tabs) To UBound(Tabs)
Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1).Name = Tabs(I)
Next I
Sheets("Draw").Range("A1:L1000").Copy
With Sheets("Draw Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
Sheets("AIN").Range("A1:L1000").Copy
With Sheets("AIN Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
Sheets("Calculations").Range("A1:L1000").Copy
With Sheets("Calculations Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Suggestion below.
Also I would avoid Option Base 1 - it's rarely used, and will just cause problems later when you get used to working with zero-based arrays.
Sub Print_copy_Current_Workbook()
Dim Tabs As Variant
Dim I As Long
Application.ScreenUpdating = False
Tabs = Array("Draw", "AIN", "Calculations")
For I = LBound(Tabs) To UBound(Tabs)
Sheets(Tabs(I)).PrintOut
Sheets.Add(After:=Sheets(Worksheets.Count)).Name = Tabs(I) & " Final"
CopyPaste Sheets(Tabs(I)).Range("A1:L1000")
Next I
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub CopyPaste(rng As Range)
rng.Copy
'this is a new sheet we're pasting to, so why not just Range("A1") ?
With Sheets(rng.Parent.Name & " Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
End Sub
I'm trying to copy a range from "Sheet1" and pasting values, not code, to the next empty row in "Sheet2". I have found this code but it only copys and pastes code:
Sheets("Sheet1").Range("G32:I54").Copy Sheets("Sheet2").Range("A60000").End(xlUp).Offset(2, 0) 'Select Case Sheets("Sheet1").Range("A1") = ""
Can anyone help pasting values and not code?
Sheets("Sheet1").Range("G32:I54").Copy
Sheets("Sheet2").Range("A60000").End(xlUp).Offset(2, 0).pastespecial paste:=xlPasteFormats
Sheets("Sheet2").Range("A60000").End(xlUp).Offset(2, 0).pastespecial paste:=xlPasteValues
application.cutcopymode=false