VBA: Copy and paste values in next empty row in other sheet - vba

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

Related

Copy formula and paste over dynamic range [duplicate]

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

Copy and PasteSpecial Table Values and Formatting

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

excel vba copy row and paste to new sheet as pastevalue

I want to copy data from sheet "summary" row A44 (fixed row with dynamic data with formula) to sheet18 (row A3), A1 and A2 are header; i have below vba code and manage to do so. I would like to copy and paste the data as value (like Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False), so that the data will convert to absolute number, anyone how to edit the code?
Sub COPY_SUMMARY2COPYDATA()
Set des = Sheet18.Range("a1")
With Worksheets("SUMMARY")
.Rows(Range("A44").Row).Copy
des.Range("A3").Insert Shift:=xlUp
End With
Application.CutCopyMode = False
End Sub
Please try this:
Sub COPY_SUMMARY2COPYDATA()
Dim LastRow As Long
LastRow = Sheet18.Cells(Rows.Count,1).End(XlUp).Row + 1
Sheets("SUMMARY").Rows("44").Copy
Sheet18.Rows(LastRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Hope this help
To find the first blank cell in column A look from the bottom up and offset down one row.
Use a direct value transfer with .Value2 to pass over 'the data will convert to absolute number'. This will discard regional currency and date conventions as well as formulas in favor of the raw underlying value.
with Worksheets("SUMMARY")
with intersect(.usedrange, .rows(44).cells)
Sheet18.cells(.rows.count, "A").end(xlup).offset(1, 0).resize(.rows.count, .columns.count) = .value2
end with
end with

Paste special method of range class failed vba

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

Error: PasteSpecial method 2013

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