This question already has answers here:
Copy Paste Values only( xlPasteValues )
(7 answers)
Closed 4 years ago.
I have code that works perfectly except for I can not seem to paste only values of the copied cell. How would I tell this code to only paste values and not merely copy and paste the cell. I have tried Google, Stack etc with no luck every variation of paste special I try fails! Thanks!
Sheets("AAAA").Select
Dim LR As Long, i As Long
With Sheets("AAAA")
LR = .Range("H" & Rows.Count).End(xlUp).Row
For i = 2 To LR
With .Range("G" & i)
If .Cells.Value = "NO MATCH" Then
With Sheets("BBBB")
ActiveSheet.Cells(i, 1).Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0)
ActiveSheet.Cells(i, 2).Copy .Cells(.Rows.Count, "C").End(xlUp).Offset(1, 0)
ActiveSheet.Cells(i, 3).Copy .Cells(.Rows.Count, "D").End(xlUp).Offset(1, 0)
ActiveSheet.Cells(i, 4).Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1, 0)
ActiveSheet.Cells(i, 5).Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1, 0)
ActiveSheet.Cells(i, 7).Copy .Cells(.Rows.Count, "H").End(xlUp).Offset(1, 0)
ActiveSheet.Cells(i, 8).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
End If
End With
Next i
End With
Be careful with nested With statements. Maybe fully qualify the second sheet in the inner With.
That said, you can remove the copy altogether and swop it round into an assignment using .Text property of range (cell) to give value only e.g.
.Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0) = ActiveSheet.Cells(i, 1).Text
Better still use .Value2
.Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0) = ActiveSheet.Cells(i, 1).Value2
It preserves more formats and is more resilient.
Related
VBA novice and got 90% of the way to what I need but I just can't figure out the final part. For the last step I have a range of data from A:K, with A containing a unique number. An updated version of this data is pasted below the initial range with the numbers in Column A staying the same, but B:K being updated.
How can i copy the duplicate row below, paste it over the original above, and then delete the duplicate?
Sub TEST2()
'
' TEST2 Macro
'
' Sheets("Sheet1").Select
ActiveSheet.Range("A1:K1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$L$20").AutoFilter Field:=8, Criteria1:="red"
Range("a2").Select
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:K" & LR).SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("Sheet2").Select
Range("A2").Select
Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
ActiveSheet.Range("A1:l100").RemoveDuplicates Columns:=Array(1, 1), Header:=xlYes
End With
Range("$q$1").Select
Selection.Copy
Range("H2:H1000").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Sheets("Sheet1").Select
Worksheets("Sheet1").ShowAllData
Range("O3").Select
Sheets("Sheet2").Select
Range("O3").Select
End Sub
At the moment i can only get as far as using this to delete the duplicates. There are other elements to the sheet which require it to be done this way.
Thanks in advance for any help!!
First thought after seeing the issue... it's a little more than a single line:
Dim i as integer, LR as Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 to LR 'Assumes that row 1 is headers
If Application.Match(Cells(i,1),Range(Cells(2,1),Cells(i-1,1)),0)>0 Then
Rows(i).Cut
Rows(Application.Match(Cells(i,1),Range(Cells(2,1),Cells(i-1,1)),0)+1).PasteSpecial xlPasteValues
Else
End If
Next i
Edit: It's not liking the range; I will try cleaning it up, then use insert/delete... keep in mind, if we're using delete for any row, you'll want to reverse the step, as to avoid issues. See below changes, noting that j is added:
Dim i As Integer, j As Integer, LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = LR To 3 Step -1 'Assumes that row 1 is headers
If Application.IfError(Application.Match(Cells(i, 1), Range(Cells(2, 1), Cells(i - 1, 1)), 0), 0) > 0 Then
j = Application.Match(Cells(i, 1), Range(Cells(2, 1), Cells(i - 1, 1)), 0)
Range(Cells(i, 1), Cells(i, 11)).Cut
Range(Cells(j + 1, 1), Cells(j + 1, 11)).Insert xlShiftDown
Range(Cells(j + 2, 1), Cells(j + 2, 11)).Delete
End If
Next i
You can use the below algorithm (with illustrated example as below) :-
Create a column to store sequential number for sorting purpose
Perform the sorting so that the latest appended rows are always at the top. Excel's removeduplication function will always keep the first encountered unique value
Once done, you can perform sorting to re-order the rows of data again.
Below is a sample code which you will need to modify based on your actual dataset.
Sub Test()
LastRow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
Range("L1").Value = LastRow
Range("L2").Value = LastRow - 1
Range("L1:L2").AutoFill Destination:=Range("L1:L" & LastRow)
Range("A1:L" & LastRow).Sort Order1:=xlAscending, Key1:=Range("L1"), Header:=xlNo
Range("A1:L" & LastRow).RemoveDuplicates Columns:=Array(1, 1), Header:=xlNo
End Sub
I have a number of files, it contains data of each and every day of a month.
I need to collect all the data to a master file and insert date for each file.
I set [the 1st day -1] in A1 cell and use the code below to set date to each day range but it is not working well. Any helps is appreciated!
Do While buf <> ""
Set openWB = Workbooks.Open(fold_path & "\" & buf)
With openWB.ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Range(.Cells(2, "A"), .Cells(LastRow, LastCol)).Copy
End With
With ThisWorkbook.Worksheets("GA")
.Range("B" & .Cells(.Rows.Count, "B").End(xlUp).Row + 1).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Range(.Cells(Cells(.Rows.Count, "B").End(xlUp).Row, "A"), .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row + 1, "A")).Value = .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, "A").Value + 1
End With
openWB.Close False
buf = Dir() Loop
Try it this way:
With ThisWorkbook.Worksheets("GA")
With .Cells(.Rows.count, "B").End(xlUp).Offset(1)
.Offset(, -1).value = .Offset(, -1).End(xlUp).value + 1
.PasteSpecial Paste:=xlPasteValues
End With
End With
My original program is able to find the Cells I need based on Month and Week now I need to modify the program to copy the last used cell in Row 5 and paste it until the end of the column.
Ex. If the month if November and it is the 4th week then the program knows to go there and fill in the information. I cannot figure out how to paste value from Nov wk 4 into the rest of the column. Also my range could be H5:BA5 or BC5:DB5 depending on where the month and week start.
I have added a picture that shows how my data is set up, the highlighted cells need to be filled in until the end
With ThisWorkbook.Sheets(SheetName)
Dim c2 As Integer
Dim LastCol2 As Integer
c2 = 2
LastCol2 = .Cells(4, .Columns.Count).End(xlToLeft).Column
Do Until .Cells(1, c2).Value = "Sept" And .Cells(4, c2).Value = "Wk 5"
If .Cells(1, c).Value = MonthSel And .Cells(4, c).Value = WkSel Then
.Cells(5, c2).Select
Selection.copy
ActiveCell.Offset(0, 1).Select
Selection.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("H5:BA5"), Type:=xlFillDefault
Range("H5:BA5").Select // need to change this range to reach the end of column 5
End If
Loop
End With
First off you need to STOP USING .SELECT!
Secondly, you already figured out how to find the last used column in your code. Why not use the same logic to find the last used row?
With ThisWorkbook.Sheets(SheetName)
Dim c2 As Long, LastCol2 As Long, LastRow As Long
c2 = 2
LastCol2 = .Cells(4, .Columns.Count).End(xlToLeft).Column
Do Until .Cells(1, c2).Value = "Sept" And .Cells(4, c2).Value = "Wk 5"
If .Cells(1, c).Value = MonthSel And .Cells(4, c).Value = WkSel Then
.Cells(5, c2).copy
.Cells(5, c2).Offset(0, 1).Paste
Application.CutCopyMode = False
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(5, c2).Offset(0, 1).AutoFill Destination:=Range("H5:BA" & LastRow), Type:=xlFillDefault
End If
Loop
End With
A much simpler solution (if we assume you have some sort of lookup formulas in your cells) is to grab all of column B and auto-fill all 52 weeks:
Sub TestingIt()
Dim LastRow As Long
With ThisWorkbook.Sheets(SheetName)
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Range("B5:B" & LastRow).AutoFill Destination:=Range("B5:BA" & LastRow), Type:=xlFillCopy
End With
End Sub
This is part of my script, easy loop copying some rows from one sheet to another:
a = 3
With Sheets("ATD")
Do While .Range("A" & a) <> ""
If .Cells(a, 6).Value = "x" And .Cells(a, 8).Value = "y" Then
.Range(Cells(a, 1), Cells(a, 10)).Copy
Sheets("ART").Range("A" & Sheets("ART").Range("A" & Rows.Count).End(xlUp).row + 1).PasteSpecial xlPasteValues
End If
a = a + 1
Loop
End With
It fails almost every time on .Range(Cells(a, 1), Cells(a, 10)).Copy line (Run-time error '1004': Application-defined or object-defined error). When I add .Select command like this:
a = 3
With Sheets("ATD")
Do While .Range("A" & a) <> ""
If .Cells(a, 6).Value = "x" And .Cells(a, 8).Value = "y" Then
.Select
.Range(Cells(a, 1), Cells(a, 10)).Copy
Sheets("ART").Range("A" & Sheets("ART").Range("A" & Rows.Count).End(xlUp).row + 1).PasteSpecial xlPasteValues
End If
a = a + 1
Loop
End With
everything works fine.
I know I can change .Copy on something like
Sheets("ATD").Range(Cells(a, 1), Cells(a, 10)).Value = Sheets("ART").Range(Cells(b, 1), Cells(b, 10)).Value
but I have another question. If .Copy function require, that cells I want to copy are in currently selected sheet, or am I missing something here?
Can you try with:
.Range(.Cells(a, 1), .Cells(a, 10)).Copy
The points are really important, as they reference the current Cells with the Sheet Object set in the With Sheets("ATD") line.
I receive Runtime Error '13': Type Mismatch when I try to run the code. Debug highlights the 'IF' statements, but I can't figure out where the mistake is. Any help would be appreciated. Thanks
Dim i As Integer
Dim lastRow As Long
Workbooks("Template Part_II.xlsx").Worksheets(2).Activate
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
If Cells(i, 1).Value <> "#N/A" And Cells(i, 1).Value <> "00000000-000" Then
Cells(i, 1).Copy
Worksheets(1).Range("A2:A" & lastRow).PasteSpecial xlPasteValues
End If
Next I
and in fact I'm trying to do this:
I have one Sheet where I have 100 Rows of various IDs and I want to copy this IDs to another sheet without possible non ID strings in this case it can be #N/A or 00000000-0000, also I don't want those non copied cells to appear as blanks in destination range.
Wrap your accesses to the cell inside a check which ensures the cell contains no error value (e.g. a cell 'containing' a division by 0) like so
...
For i = 2 To lastRow
If Not IsError(Cells(i, 1).Value) Then
If Cells(i, 1).Value <> "#N/A" And Cells(i, 1).Value <> "00000000-000" Then
Cells(i, 1).Copy
Worksheets(1).Range("A2:A" & lastRow).PasteSpecial xlPasteValues
End If
End If
Next i
...
Note: I tried to insert the condition at the front of the existing If but it seems VBA does not use short-circuiting therefore the wrapping
Update due to comment
You may want to change your code like this
Dim i As Integer
Dim lastRow As Long
Dim targetRow As Long
Workbooks("Template Part_II.xlsx").Worksheets(2).Activate
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
targetRow = 2
For i = 2 To lastRow
If Not IsError(Cells(i, 1).Value) Then
If Cells(i, 1).Value <> "#N/A" And Cells(i, 1).Value <> "00000000-000" Then
Cells(i, 1).Copy
Worksheets(1).Cells(targetRow, 1).PasteSpecial xlPasteValues
targetRow = targetRow + 1
End If
End If
Next i