Excel VBA - Copy range from one sheet to another, next empty row - excel

I'm trying to take a range from one sheet and copy it to the next empty row in another sheet (basically, paste into the range A12:D12 for next empty row in the other sheet). The range will never change. I've seen a lot of questions like this, with people saying the answers work great, but I can't get it to work.
Very new to VBA. Here is the code I'm using:
Private Sub CommandButton1_Click()
Dim NextRow As Range
Set NextRow = Range("A" & Sheets("Sheet3").UsedRange.Rows.Count + 1)
Sheet1.Range("A12:D12").Copy
Sheet3.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
This runs but it doesn't actually paste any values into Sheet3. Is there something I'm missing? Is there a better way to do this?
Thanks!

You just had an issue in the second line defining NextRow.
Is there a better way to do this? It depends on your needs; personally I do not like to activate/select other cells during a VBA macro so e.g. I would get rid of the Sheet3.Activate. I would also copy the stuff 'manually' without using the clipboard to avoid changing the user's clipboard contents.
Private Sub CommandButton1_Click()
Dim NextRow As Range
Set NextRow = Sheet3.Range("A" & Sheet3.Rows.Count).End(xlUp).Offset(1, 0)
Sheet1.Range("A12:D12").Copy
Sheet3.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub

Related

VBA Copy and pastevalues in another sheet using offset

I want to create an excel with a macro which does this:
It takes a values from one sheet and paste them in another sheet in the first empty Row. I currently have this and this does paste but then the formula, not the values. When I change Paste to Pastespecial it does not work anymore. Can anybody help me with this?
Sub CopyPasteSpecial()
Set wsA = Sheet3
Set wsW = Sheet5
wsA.Range("A3").Copy
Sheet5.Activate
Dim i As Integer
Application.ScreenUpdating = False
NumRows = Range("C5", Range("C5").End(xlDown)).Rows.Count
Range("C5").Select
For i = 1 To NumRows
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
ActiveSheet.Paste
The (accepted) answer of Sachin Kohli contains so many problems that it is too much to write in a comment. All of them causes errors that can be found thousend times on Stackoverflow.
(a) Don´t use Select and Activate - https://stackoverflow.com/a/10718179/7599798 - You have already a worksheet variable to sheet5 (wsW), so use it.
(b) Copying data (values) between ranges (=cells) can be done without Copy&Paste. This is much faster.
(c) When dealing with row and column numbers, use datatype Long, else you risk an overflow error in large sheets. Basically, use always Long instead of Integer - the data type Integer exists only for historic reasons.
(d) Always put Optiona Explicit at the top of your code and declare all variables. This will save you a lot of headaches because typos in variable names will be caught by the compiler.
Sub CopyPasteSpecial()
Dim wsA As Worksheet, wsW As Worksheet
Set wsW = ThisWorkbook.Sheets("Sheet2")
Set wsA = Sheet1
Set wsW = Sheet2
Dim lastRow As Long
lastRow = wsW.Cells(wsW.Rows.Count, "C").End(xlUp).Row
wsW.Cells(lastRow + 1, "C").Value = wsA.Range("A3").Value
End Sub
Try this below code for pasting as values & no need to iterate to get to the last empty row as well...
Sub CopyPasteSpecial()
Set wsA = Sheet3
Set wsW = Sheet5
wsA.Range("A3").Copy
Sheet5.Activate
Dim i As Integer
Application.ScreenUpdating = False
NumRows = Range("C5", Range("C5").End(xlDown)).Rows.Count
Range("C" & NumRows + 5).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Hope this Helps...

VBA code for Pasting below existing data not working, if it has value in single row

I am writing VBA code to paste data below already existing data, which i have earlier done as well. this time code is pasting data only when it has value in more than 1 row. If it has value in single row it will copy, but wont paste. Code is below, any help is really appreciated:
range(range("a1:c5"), range("a:c").End(xldown)).select
selection.copy
sheets("finalNo").Cells(Rows.count, 1).End(xLUp).offset(1,0).Pastespecial xlpastevalues
this code will not paste anything if the value is in single row only, but will paste perfectly if its in more than two rows. Unable to solve this. Please help.
Give it a try here:
Sub TestMe()
Dim i As Long
With Worksheets("Sheet1")
i = LastRow(.Cells.Parent)
.Range(.Cells(1, 1), .Cells(i, "C")).Copy
Sheets("finalNo").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End With
Application.CutCopyMode = False
End Sub
Public Function LastRow(ws As Worksheet, Optional columnToCheck As Long = 1) As Long
LastRow = ws.Cells(ws.Rows.Count, columnToCheck).End(xlUp).Row
End Function
The "magic" comes from using the function LastRow(), which gives the last used row in the worksheet, named "Sheet1".
The .Cells.Parent refers to the worksheet om the With-clause. But instead it could be written like this i = LastRow(Worksheets("Sheet1")), which is the same.

VBA paste value, format number and theme

i have a little problem with VBA. I want to paste data from one sheet and specific range to another sheet and last empty cell. I need to paste Value, Number format and theme. Is there any macro to select last paste range and paste into this range theme? Or is there any macro which can paste all data without formulas?
My code:
Dim xScreenUpdating As Boolean
Dim xPasteSht As Worksheet
Dim xRg As Range
Dim xTxt As String
On Error Resume Next
Worksheets("KOPÍROVAT ASISTENT").Range("C1:G54").Copy
Set xPasteSht = Worksheets("Archiv")
xRg.Copy
xPasteSht.Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).PasteSpecial xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
I'm not sure what .Offset(0,0) achieves - .Offset(1,0) is what you actually need to find the last empty cell. That aside, the following code snippet should give you what you want. Let me know how you go with it.
Sheets("KOPÍROVAT ASISTENT").Range("C1:G54").Copy
With Sheets("Archiv").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With

.End(xlUp).select - selecting next column not row

I'm trying to write code to find the next free row in a work book to copy 4 cells of data from one workbook to another.
The code I've used works fine when I run it first time round (and there's nothing in the workbook). It selects A2 and pastes in the 4 cells of data. However when I try to run the same macro again, it selects B2, instead of A3?
I've used this function multiple times before but I've never seen anything like this before. My code is below.
'
' Macro6 Macro
'
Dim fRow As Long
With ActiveSheet
fRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(fRow).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
End With
End Sub
The issue is that Cells needs a row and column like .Cells(fRow, "A")
Option Explicit
Public Sub PasteRows()
With ActiveSheet
Dim fRow As Long
fRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(fRow, "A").Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End With
End Sub
also don't use .Select it is a bad practice: You might benefit from reading
How to avoid using Select in Excel VBA.
Alternatively use the following which is even shorter:
Option Explicit
Public Sub PasteRows()
With ActiveSheet
.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End With
End Sub

how to paste only values in excel

I'm quite new to Excel and completely new to this forum.
I have taken the code from the below forum and modified it to my need.
http://pressf1.pcworld.co.nz/showthread.php?90122-Creating-Macro-to-copy-and-paste-data-into-the-next-empty-column.
Sub copyTotals()
Dim TargetSht As Worksheet, SourceSht As Worksheet, SourceRow As Integer, SourceCells As Range
Set SourceSht = ThisWorkbook.Sheets("DUN - Jan")
Set TargetSht = ThisWorkbook.Sheets("DUN Jan - Jan,Feb,Mar,Apr")
Set SourceCells = SourceSht.Range("L36,N36")
If TargetSht.Range("C11").Value = "" Then
SourceRow = 1
ElseIf TargetSht.Range("C41") <> "" Then
MsgBox ("The sheet is full, you need to create a new sheet")
Else
SourceRow = TargetSht.Range("C41").End(xlUp).Row + 1
End If
SourceCells.Copy TargetSht.Cells(SourceRow, 3)
End Sub
The problem is that the values pasted have the formating of the source and i only want to paste the values.
Can someone please help me with this.
Use .Copy together with .PasteSpecial. Instead of:
SourceCells.Copy TargetSht.Cells(2, SourceCol)
Do this:
SourceCells.Copy
TargetSht.Cells(2, SourceCol).PasteSpecial xlPasteValues
Using the macro recorder yields this kind of thing. I use it a lot when stuck.
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
You don't need to copy and paste to do this (at least in Excel 2010 and above, I think). You just set the .Value of the range to equal itself. eg:
rng.Value = rng.Value
or
Sheet1.Range("A1:A10").Value = Sheet1.Range("A1:A10").Value
since .Value "Returns or sets a Variant value that represents the value of the specified range."
Note that this just works for values, not formats.
http://msdn.microsoft.com/en-us/library/office/ff195193(v=office.15).aspx
right click in the cell and select paste special then you can choose values only
edit: for macros its the same principle
use
.PasteSpecial xlPasteValues
like here
Set rng6 = .Range("A3").End(xlDown).Offset(0, 41)
rng6.Copy
ActiveSheet.Paste Destination:=Worksheets("Positions").Range("W2")

Resources