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

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

Related

Excel VBA Moving Row Values [Only] from one Sheet to another - Current code is working, just needs tweaking

This is currently the setup that I have found helpful and have modified to work well... However, I'm struggling with one small further and final modification. I would like to just - Paste Values as opposed to the Formulas.
Sub move_rows_to_another_sheet()
'
Sheets("User").Select
Columns("A:Y").Select
Range("A:Y").Activate
'
For Each myCell In Selection.Columns(25).Cells
If myCell.Value = "Closed" Then
myCell.EntireRow.Copy Worksheets("Archive").Range("A" & Rows.Count).End(3)(2)
myCell.EntireRow.Delete
End If
Next
'
Range("A2").Select
End Sub
''Updated Version - Move Single Rows
'
Sub move_rows_to_another_sheet()
'
Sheets("Users").Select
Columns("A:Y").Select
Range("A:Y").Activate
'
For Each mycell In Selection.Columns(25).Cells
'
If mycell.Value = "Closed" Then
mycell.EntireRow.Copy
Worksheets("Archive").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
mycell.EntireRow.Delete
End If
Next
'
Range("A2").Select
End Sub
The idea is simple... The current code is successful, however, I would like to just copy and paste the [values] of the rows cell content and [not] the formulas etc. The formatting is fine and everything, I just need the result of the functioning formulas recorded.
I have tried various options such as [myCell.EntireRow.CopyValues] even [& Rows.Count & Rows.PasteSpecial]... Any thoughts?
Thanks in advance
I tried your code. It looks like when there are several cells with "closed" it will not work for all. because when one deletes, one should delete from below upwards.
But then the data in Archive is not in right order.
In your original code you can make the range smaller, so it will run faster.
or take what you want from this code:
Sub move_rows_to_another_sheet2()
Dim mycell As Range
Dim checkClosed
Dim Lastrow As Long
Dim i As Long
Set checkClosed = ThisWorkbook.Worksheets("User").Range("Y1:Y10000")
Lastrow = Worksheets("Archive").Cells(Rows.Count, 1).End(xlUp).Row + 1 'one cell below last used cell in column A
For i = 10000 To 1 Step -1 'from row 10000 to row 1
Set mycell = ThisWorkbook.Worksheets("User").Cells(i, "Y")
If LCase(mycell.Value) = "closed" Then 'checks for Closed and closed
mycell.EntireRow.Copy
Worksheets("Archive").Range("A" & Lastrow).PasteSpecial Paste:=xlPasteValues
mycell.EntireRow.Delete
Lastrow = Lastrow + 1
End If
Next i
'
Range("A2").Select
End Sub

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.

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

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

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

Copy and Paste Issue

This code works two or three times, and then I get a "PasteSpecial method of Range class failed" error. I know I get that error when nothing is copied, but considering it's copied right above, I don't understand why it's not working.
When I debug and watch it step by step, it loops back to the beginning after the Paste line, rather than running through the end of the Sub.
Sub AddRows()
Range("A11").End(xlDown).EntireRow.Copy
Range("A11").End(xlDown).Offset(1, 0).PasteSpecial xlPasteAll
Application.CutCopyMode = False
End Sub
Any ideas?
Thank you all in advance!
Try below code : The copy and paste operation can be combined in 1 line.
Below code copies the range from cell A11 till the last cell which has the data and paste the data in very next cell below.
Sub AddRows()
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim rng As Range
Set rng = Range("A11:A" & lastRow)
' Next row
rng.Copy Cells(lastRow + 1, 1)
'if you want data to be pasted to Column B use below
'rng.Copy rng.Offset(0, 1)
End Sub

Resources