Copy formula and paste over dynamic range [duplicate] - vba

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

VBA Copy and Paste Macro which Loops

New to VBA and simply wanted to create a macro which copies the tables within a specified range and pastes in the next available empty rows. What happens is that every time I run it it pastes into the same range i.e. B12 and don't know how to amend...
Sub CopyRange2()
Range("A1:I9").Select
Selection.Copy
Range("B12").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Would like to know why you have specified cell B12 ??
The below code will work if you just want to paste the data in the next available empty rows.
Sub CopyRange2()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1:I9").Select
Selection.Copy
Range("A" & lastrow + 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub

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

VBA code for copying and pasting content in multiple worksheets

Please help me with a macro for copying and pasting text from Input sheet (worksheet1) to the Reports sheet (worksheet 2) within the same excel file.
Input Sheet Format:
Name- cell J5
Date- cell J6
Start Time- cell J7
End Time- cell J8
Downtime- cell J9
I need a macro for copying J5:J9 and paste it in A2:E2, using transpose function.
Also, the sheet is shared with multiple users so it needs to be pasted in the last available row in the Reports sheet (worksheet 2).
Presently, I am using the following macro code:
Sub Report()
Sheets("Input").Select
Range("J5:J9").Select
Selection.Copy
Sheets("Reports").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Sheets("Input").Select
ActiveWindow.SmallScroll Down:=-5
Sheets("Input").Select
Range("J5:J9").Select
Selection.ClearContents
End Sub
I am getting error at "ActiveCell.Offset(1, 0).Select" while running the macro.
Please advise any changes to the code which can help me fixing the issue.
Thanks
You can rewrite everything you're doing in a few lines:
Sub Report()
' Copy the range from J5:J9 on INPUT worksheet
ThisWorkbook.Worksheets("Input").Range("J5:J9").Copy
With ThisWorkbook.Worksheets("Reports")
' Find the LAST ROW in COLUMN A
Dim lROW As Long: lROW = .Cells(.Rows.Count, 1).End(xlUp).Row
' Paste in cell A-lROW with transpose
.Range("A" & lROW).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, Transpose:=True
End With
' Clear entry in original INPUT sheet
ThisWorkbook.Worksheets("Input").Range("J5:J9").ClearContents
End Sub
I'm not sure why you're getting an error for offsetting by one row, it works for me - but it's probably to do with your method of "selecting" the last row

VBA copy formatting from a row to several rows

Alright so I am really close to getting this but I am just trying to make it work better. I want to copy row 2 formatting that goes until like Column H. The data only goes until Column H. So my code copies ONLY row 2 until Column H. But when it goes to paste, it highlights the whole sheet besides row 1 and it looks like it copies the formatting across the whole thing. It is not really an issue but I would rather know how to make it paste only in the rows and columns I want for future reference. I only want it going to cells that have data in it basically. Thanks for the help in advance!
Range("A2", Cells(2, Columns.Count).End(xlToLeft)).COPY
Range("A2", Cells(Range("A" & Rows.Count).End(xlDown).Row)).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Try this:
Dim rngCopy As Range, rngPaste As Range
With ActiveSheet
Set rngCopy = .Range(.Range("A2"), .Cells(2, Columns.Count).End(xlToLeft))
Set rngPaste = .Range(.Range("A2"), _
.Cells(Rows.Count, 1).End(xlUp)).Resize( , rngCopy.Columns.Count)
End With
rngCopy.Copy
rngPaste.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False

VBA Paste Data After Last Row Issue

I am compiling data from multiple worksheets to place on one consolidated sheet. The first sheet is easy because I can paste the entire sheet however after that it becomes tricky as I only need from A5-H### as I no longer need the header. I then need to paste this information at the bottom of the previous paste. I am getting a Range of Object "_Global" failed for the 'Range(lastRow).Select'. The issue is when I look at it in the debugger it is coming up with the correct row number. What am I doing wrong?
Sheets(1).Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Consolidation Sheet").Select
Range("A1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("A5:A925").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Consolidation Sheet").Select
Range("A5").Select
lastRow = ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Row + 1
Range(lastRow).Select
ActiveSheet.Paste
You need your Range statement in the following form (for example):
Range("A1").Select
So since you've got the row number (lastRow), you just need to add the column reference too:
Range("E" & lastRow).Select
Extra info
Avoid using Select by referring directly to the worksheets/ ranges you want to deal with
Sheets(1).Cells.Copy Destination:=Sheets("Consolidation Sheet").Range("A1")
With Sheets(2)
.Range(.Range("A5:A925"),.Range("A5:A925").End(xlToRight)).Copy
End With
With Sheets("Consolidation Sheet")
.Cells(.Rows.Count, "E").End(xlUp).Offset(1,0).PasteSpecial
End With