Why Selection paste is not working on VBA [duplicate] - excel

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

Related

Run Time error '438' when tried to copy and paste filtered result in VBA [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

I am getting a VBA run time error 438 when using ThisWorkbook [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

cannot copy and paste when area size is not the same

Dim lastrow&, lastCol&, myarray As Range
lastrow = Range("A1").End(xlDown).Row
lastCol = Range("XX1").End(xlToLeft).Column
Set myarray = Range("A1").Resize(lastrow, lastCol)
Range("A1", myarray).Select
Selection.Copy
So basically, i am trying to get select an array which could vary, I know it starts at A1, but I'm unsure which row and column it will end at. Code above works fine to help copy this array.
Application.CutCopyMode = False
Selection.Copy
Application.WindowState = xlNormal
Windows("macrofile.xlsm").Activate
Sheets("MRG").Select
'has to find the last row by itself
Range("A" & Rows.Count).End(xlUp).Offset(2, 0).Select
ActiveCell.PasteSpecial (xlPasteAll)
I am getting an error on the last line ActiveCell.PasteSpecial (xlPasteAll).
Error 1004, can't paste because copy area and paste area aren't the same
I have tried different variations including activesheet.paste and xlpastevalues to no avail.
Range("A" & Rows.Count).End(xlUp).Offset(2, 0).Select selects a single cell in column A to find the last used row and offsets it by 2 rows so I can paste below the existing data. Not sure why error 1004 comes up because replicating selecting an array and pasting it into a single cell in excel runs no errors.
Any help is much appreciated; I am really new to VBA and most of this code came from different sources online.
As long as the source data has no blank rows or columns you can do this:
ActiveSheet.Range("A1").Currentregion.Copy _
Workbooks("macrofile.xlsm").Sheets("MRG").Cells(Rows.Count, "A").End(xlUp).Offset(2,0)
Assuming there's room for the pasted data.

MAC VBA Trying to cut one cell from each worksheet and paste in another sheet in next empty cell

I am trying to go through each worksheet in the active workbook in which the sheets are created as part of a macro that is working. The names can be different each time, so I can't just do a lookup. I want to take one piece of information (same cell on each worksheet) and paste it into the next empty cell in a column on another sheet.
At line 6 I get the error subscript out of range error 9. I've checked the naming of the sheet I am pasting to and it is correct. No extra spaces.
It's driving me nuts. Can you help?
ps I am working on this to run on a MAC which I normally don't do, so maybe I have the code slightly wrong.
For Each sh In ThisWorkbook.Worksheets
DoEvents
sh.Activate
Range("K5").Select
Selection.Copy
Sheets("Payment Ref").Range("b2").Select
If Range("b2") = "" Then
Range("b2").PasteSpecial Paste:=xlPasteFormats
Range("b2").PasteSpecial Paste:=xlPasteValues
Else
Range("b2").Offset(1, 0).PasteSpecial xlPasteFormats
Range("b2").Offset(1, 0).PasteSpecial xlPasteValues
End If
Next sh
At the end of the run the Payment Ref Spreadsheet should have a column filled with invoice numbers from the invoice sheets created in the previous macro section. This works perfectly.
Avoid .Select and .Activate (see How to avoid using Select in Excel VBA) and reference a worksheet for all your ranges (eg using With).
Also you need to find the last used cell everytime you paste. A good way to find the last used cell in a column is:
Cells(Rows.Count, "B").End(xlUp) 'last used cell in column B
So the following should work for you
Dim wsDestination As Worksheet
Set wsDestination = ThisWorkbook.Worksheets("Payment Ref")
For Each sh In ThisWorkbook.Worksheets
sh.Range("K5").Copy
With wsDestination.Cells(wsDestination.Rows.Count, "B").End(xlUp).Offset(RowOffset:=1)
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteValues
End With
Next sh
You should avoid using .Select in general, as linked in the comments. This is most likely the cause for your error. Also, if you perform this code on more than 1 or 2 worksheets (depending on if B2 already has a value) the procedure will keep putting the values in cell B3. I'd suggest the following changes:
For Each sh In ThisWorkbook.Worksheets
sh.Range("K5").Copy
Sheets("Payment Ref").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteFormats
Sheets("Payment Ref").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Next sh
I think this should work just as well:
For Each sh In ThisWorkbook.Worksheets
sh.Range("K5").Copy Sheets("Payment Ref").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
Next sh

VBA:Excel how to copy including formatting and spacing

Right now, I'm doing this to copy a row from one sheet to another sheet and insert it at the end:
Sheets("Monthly").Range("A1").EntireRow.Copy Destination:= _
Sheets(Name).Range("A" & rows.Count).End(xlUp).Offset(1)
But this doesn't copy any formatting/cell spacing. Is that possible to do in vba?
Copy does copy the formatting. Maybe your formatting isn't what you think. I don't know what cell spacing means, but if it's column width, then Copy won't copy that. You'll have to set that explicitly, like
For i = 1 to rSrce.Cells.Count
rDest.Cells(i).EntireColumn.ColumnWidth = rSrce.Cells(1).EntireColumn.ColumnWidth
Next i
Using the Macro Recorder will give you the syntax for other formats.
Range("A1").NumberFormat = "General"
Or
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteValues
Ex. of a way to solve it
Sub Copy()
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
ws.Range("A1").EntireColumn.Copy
With ws.Range("G2").EntireColumn
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteValues
End With
End Sub
There are a million ways to do this try
https://www.google.com/search?q=how+to+copy+formatting+in+excel+with+vba&oq=how+to+co&aqs=chrome.0.69i59l2j69i57j69i59j69i60j0.2240j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8

Resources