The intent is to:
On Sheet(Master), update a Cell which triggers other cells to update a specific range.
On Sheet(Master), select updated range and copy that range.
On Sheet(Paste), paste the values and formatting of the copied data below the last row of data.
The script below functions properly, except for the paste special portion: PasteSpecial Paste:=xlPasteValues.
Private Sub CommandButton1_Click()
'/ I am trying to Add Data From Sheet("Master") To Sheet("Paste") as a "PasteSpecial Paste:=xlPasteValues"
Dim Lastrow As Long
Sheets("Master").Range("N3") = Sheets("Master").Range("N3") - Sheets("Master").Range("N4")
Lastrow = Sheets("Paste").Range("A65536").End(xlUp).Row + 1
Sheets("Master").Range("L13:AO17").Copy Destination:=Sheets("Paste").Range("A" & Lastrow)
End Sub
I have attempted to add the PasteSpecial Paste:=xlPasteValues portion as in the examples below:
Example One.
Lastrow = Sheets("Paste").Range("A65536").End(xlUp).Row.PasteSpecial Paste:=xlPasteValues
Example Two.
Sheets("Master").Range("L13:AO17").Copy Destination:=Sheets("Paste").Range("A" & Lastrow).PasteSpecial Paste:=xlPasteValues
Those are the only two types of options that I have attempted based on online research.
After reworking it a few times, the following solution worked.
Range("N3") = Range("N3") - Range("N4")
Range("L13:AO17").Copy
Sheets("Paste").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
I decided to step away from using the Lastrow var, when I realized that it was not needed. Once that was eliminated, I then focused on cleaning the script and used the offset function instead of row. This then allowed for me to add the PasteSpecial function properly.
Related
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.
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
I have spent the day trying to understand what is going on with my excel.. I am running some code which worked fine before, I modified part of it early up (but which still works fine), and now a Selection.Copy later on in the code has stopped working. Here it is :
Range("AE3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
I used break points to find the problem. After the first two lines of code, it is the data in column AE which is selected. When I move on to the last line (Selection.Copy), it is not the data in AE which is selected but the columns AA and AB. I have tried literally everything I can think of to try and fix this but can't find anything..
If I run the code up to this point of the code and do the selection and copying manually, it also copies the wrong cells (it copies AA and AB like when it's done with vba)
I would post screenshots of it but you can't put photos here it seems.
Thanks for your help!
Resolved:
I went through the code step by step and noticed that previously in the code I copied the data from columns AA and AB to lower columns. To do so I had selected the columns and then copied them. I changed that so that I selected only the data in the columns and not the columns themselves and copied the data. This change has made my code work. I'm not sure why this was effecting the later Selection.Copy, but it was in some way. Thank you everyone for their help!
As I've suggested in my comment, avoid using .Select & Selection, is usually bad practice and almost everything can be done in VBA without the need to use them. I understand those are a result of the recorder (which is a good place to start learning how to do certain things in VBA), just need to learn as well how to use the code generated by the recorder.
See if this helps (see comments in code as well):
Sub copyRange()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1") 'use a variable for the sheet you want to use
Dim lRow As Long
lRow = ws.Cells(Rows.Count, "AE").End(xlUp).Row 'get the last row at the desired column
With ws
.Range("AE3:AE" & lRow).Copy _
Destination:=.Range("AE3:AE" & lRow).Offset(0, -10) 'destination offset 10 columns to the left
'or alternatively specify the destination
'Destination:=.Range("U3:U" & lRow)
End With
'ALTERNATIVE to the above - copy values only
With ws.Range("AE3:AE" & lRow)
.Offset(0, -10).Value = .Value 'destination offset 10 columns to the left
'or alternatively specify the destination
'ws.Range("U3:U" & lRow).Value = .Value
End With
'2nd ALTERNATIVE to the above - copy values only
With ws.Range(ws.Cells(3, 31), ws.Cells(lRow, 31))
.Offset(0, -10).Value = .Value 'destination offset 10 columns to the left
'or alternatively specify the destination
ws.Range(ws.Cells(3, 21), ws.Cells(lRow, 21)).Value = .Value
End With
End Sub
Note the use of With statement, .Range(...) is not the same as Range(...).
In case you want to copy all in column AE try this:
Range("EA3:EA" & Range("EA" & Rows.Count).End(xlUp).Row)).Copy
And to paste you could use:
Range("U3").PasteSpecial (xlPasteValues)
Also, I strongly suggest you to read:
How to avoid using Select in Excel VBA
You could try:
Option Explicit
Sub test()
Dim LastRow As Long
'Create a with statement refer to the sheet where your data are
With ThisWorkbook.Worksheets("Sheet1")
'Find the LastRow of column AE
LastRow = .Cells(.Rows.Count, "AE").End(xlUp).Row
'Refer to the range starting from AE3 and ends at Lastrow
.Range("AE3" & ":AE" & LastRow).Copy
End With
End Sub
Results:
Right now I am doing this:
xlwb.Sheets(curSheet).Cells.Copy
xlwb.Sheets(curSheet).Cells.PasteSpecial (xlValues)
This lets me copy/paste the entire sheet in-place as-values.
However I technically only want to do this for all columns between row 1 and some row near the bottom, call it finalRow. There is some content after that I wish to leave as formulas.
How can I copy/paste the rows from 1 to finalRow as-value? I'm technically running this code from Access VBA but this code is for Excel.
You can use the following syntax
xlwb.Worksheets(curSheet).Rows(1 & ":" & lastRow).Copy
xlwb.Worksheets(curSheet).Range("A1").PasteSpecial xlValues
Application.CutCopyMode = False
Change the paste range as required.
Skip the clipboard when copying only values:
With xlwb.Sheets(curSheet).Rows("1:" & lastrow)
.Value = .Value
End With
I have a membership roster that I am keeping for a chapter in a club. Rather than delete members who are no longer in the chapter, I decided to try and create a macro that looks at the Chapter Roster Master sheet in column A (Still in Chapter?) for a "yes" value and then transfers the all the rows with the yes value to another sheet called "Chapter Roster Actual".
The macro works but I would like to only transfer columns B through O and not include Column A.
I realize the one line actually tells the macro to copy the "entire row" and I have tried to have it copy only a range but in doing that, it disregards the request to only copy rows where column A has a 'yes' value. I have the range line in there as well so you could see what I tried.
I also need to figure out how to not append the rows to previously copied rows. So, I guess it should clear the rows previously populated and then write the new rows.
Here is the macro:
Sub ActualRoster()
Dim myRow, LastRow
myworksheet = "Chapter Roster Master"
LastRow = Sheets(myworksheet).Range("A" & Rows.Count).End(xlUp).Row
For myRow = 3 To LastRow
If Sheets(myworksheet).Cells(myRow, "A").Value = "Yes" Then
Sheets(myworksheet).Cells(myRow, "A").EntireRow.Copy Destination:=Sheets("Chapter Roster Actual").Range("A" & Rows.Count).End(xlUp).Offset(1)
'Sheets(myworksheet).Range("B3:O32").Copy Destination:=Sheets("Chapter Roster Actual").Range("A3:O32").End(xlUp).Offset(1)*
End If
Next myRow
End Sub
In what you tried, you are copying all the rows starting from 3 right upto 32 even if only current row is 'Yes'
The following works fine
Sub ActualRoster()
Dim myRow, LastRow
myworksheet = "Chapter Roster Master"
Sheets("Chapter Roster Actual").Range("A3").CurrentRegion.Offset(2,0).ClearContents
Sheets(myworksheet).Activate
LastRow = Sheets(myworksheet).Range("A" & Rows.Count).End(xlUp).Row
For myRow = 3 To LastRow
If Sheets(myworksheet).Cells(myRow, "A").Value = "Yes" Then
Sheets(myworksheet).Range(Cells(myRow,"B"),Cells(myRow,"O")).Copy Destination:=Sheets("Chapter Roster Actual").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next myRow
End Sub
As suggested in a comment, you can also try filter the Master data and copy all required data in one shot. As a starting point, record a macro and you will get a feel of how that can be done. Come back to refine recorded code.
You can use below code If you want to try using the filter instead of iterating over all the records. You can check which works best for you and use.
Sub ActualRoster()
Dim myRow, LastRow
myworksheet = "Chapter Roster Master"
LastRow = Sheets(myworksheet).Range("A" & Rows.Count).End(xlUp).Row
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("A2").Select
ActiveSheet.Range("A2", Range("A2").End(xlToRight)).AutoFilter Field:=1, Criteria1:="Yes"
Range("A2").End(xlToLeft).Select
ActiveCell.Offset(1, 0).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets("Chapter Roster Actual").Range("A" & Rows.Count).End(xlUp).Offset(1)
End Sub
Note: There might be more optimized code for this scenario too
Tip: You can learn how macro works by opening the VBA along with Excel (in side by side mode), record the macro and observe the code generated.