I have a range ("B56:J62") which contains some empty rows.
I tried to copy using the command below but it is copying the empty rows too.
How can I copy only the rows with value within that range.
Sub Prod_Drilling_DS()
'PURPOSE: Paste to D&B Sheet
'Copy A Range of Data
Worksheets("Production Sheet").Range("B55:J62").Copy
'PasteSpecial Values Only
Worksheets("Production Drilling").Range("B65500").End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
'Clear Clipboard (removes "marching ants" around your original data set)
Application.CutCopyMode = False
End Sub
The solution
You can qualify the cells you want to copy with SpecialCells(xlCellTypeConstants), like this:
Sub Prod_Drilling_DS()
'PURPOSE: Paste to D&B Sheet
'Copy A Range of Data (copy only cells containing data)
Worksheets("Production Sheet").Range("B55:J62").SpecialCells(xlCellTypeConstants).Copy
'PasteSpecial Values Only
Worksheets("Production Drilling").Range("B65500").End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
'Clear Clipboard (removes "marching ants" around your original data set)
Application.CutCopyMode = False
End Sub
The reason
The relevant part of the code was in which cells to copy.
(In this case, you only wanted to copy cells that contain values).
You had also specified a special type of paste, pasting "values".
This method of pasting converts all content (formulas and values) to values.
That is, any formulas are replaced by their results which are then 'hard coded' in when pasted.
Although it specifies how to paste the copied items, it does not filter which items that are to be pasted.
In order to filter what is pasted, in this instance you actually needed to filter what is copied. The above solution achieves this.
Extending the solution
If you want to include cells with either values or formulas, you would need to copy cells of type xlCellTypeConstants and also of type xlCellTypeFormulas. This answer can be modified to achieve that if needed.
Related
I am attempting to copy a specific region's contents and copy the Current Region back to a summary page.
I have unfortunately run into a bit of an issue with pasting the data back to the summary sheet.
Desired Outcome
Paste the following from my copied region to the summary page:
Maintain Merged Cells
Maintain Cell Highlight Colors
Cell Values
Copy Source
The source area has some formulas within it to gather some sheet data. See Picture Below:
Now whenever I go to paste the selected region, via means of my vba code
TitleBlockRange.CurrentRegion.Copy
nextEmptyCell.PasteSpecial (xlPasteFormats)
nextEmptyCell.PasteSpecial (xlPasteValues)
I get the following error:
I have tried every single variation of PasteSpecial() that I can think of!
Is there a method that I am missing here?
What method should I be using?
Thank you all in advance!
The curse of merged cells. But if you are stuck with them the code below will copy everything - just slower.
Sub CopyEverything()
' 153
Dim SrcRng As Range ' Source range
Dim Target As Range ' destination range
Dim i As Long ' loop counter: index of SrcRng.Cells
Set SrcRng = ActiveSheet.Cells(1, 1).CurrentRegion
Set Target = Worksheets("Sheet2").Cells(10, 1) _
.Resize(SrcRng.Rows.Count, SrcRng.Columns.Count)
Application.ScreenUpdating = False
With SrcRng
.Copy
Target.PasteSpecial xlPasteFormats
For i = 1 To .Cells.Count
With .Cells(i)
If .Address = .MergeArea.Cells(1).Address Then
Target.Cells(i).Value = .Value
End If
End With
Next i
End With
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub
Bear the following in mind.
To copy row heights you must copy entire rows.
To copy column widths you must copy entire columns.
The above task would be easier if you could copy entire rows or entire columns, or the entire sheet. However, if you deploy the above code you already have the infrastructure to copy individual settings and it shouldn't take more than a few extra lines to add a transfer of those dimensions which are important to you.
As per my knowledge, there is no way to copy complete range & paste with same formatting (merged cells, highlight colours & values). To meet requirements, you can copy & paste merged cells individually & un-merged cells separately.
For merged cells, use below codes
Range("B4").MergeArea.Copy
range("").Pastespecial
Using Pastespecial without any conditions will paste xlformats, xlvalues.
So it seems that if you use :
nextEmptyCell.PasteSpecial (xlPasteAll
nextEmptyCell.PasteSpecial (xlPasteValues)
It will throw error '1004' as shown in the original post, however if you change it to the following:
nextEmptyCell.PasteSpecial (xlPasteAll)
nextEmptyCell.PasteSpecial (xlPasteValuesAndNumberFormats)
We are able to overwrite the formulas from the xlPasteAll
The only other issue now is the annoyance of the following "pop-up" whenever we get to the nextEmptyCell.PasteSpecial (xlPasteValuesAndNumberFormats) line of code.
I'm attempting to find a column using the column header name, then select all the data from the column (including the blank cells) & paste into another range.
Currently I can only copy until the 1st blank cell. I have seen similar problems on the board but the solutions I have seen are coming from the angle of knowing which column it is in first & then finding the last row from the bottom of the worksheet.
Workbooks("PS & Config - Actuals & FC.xlsm").Worksheets(2).Range("A3").CurrentRegion.Find(What:="FFA Name").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Range("A3").Select
ActiveSheet.Paste
To find the column use Match wit the column header name. Once you have that column, set the values in the destination range to be the same as that column. Selecting, copying, and pasting in VBA are unnecessary as the values themselves can be moved--plus it adds load.
I don't have Office anymore so I'm running this from memory. I hope it helps.
dim wb as workbook, ws1, ws2 as worksheet, myCol, myRow as long
set wb=excel.thisworkbook 'assuming this code goes in that workbook
set ws1=wb.sheets(2) 'set variables
myCol=worksheetfunction.match("FFA Name",ws1.[a1:zz1],0) 'search through 1st row
myRow=ws1.[a3].currentregion.rows.count 'grab last row containing data in this set
'do not select, nor copy and paste, if it can all be done with VBA. This can.
set ws2=wb.sheets.add 'a little rusty on this line, you can get the method from the macro recorder
ws2.cells(1,1).resize(myrow).value=ws1.cells(1,myCol).resize(myrow).value
The reason your original code stops when there's a blank cell is because the .end() method simulates pushing the end key and then an arrow, which goes to the first/last contiguous cell with data.
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Having a small issue. I have a large piece of code that takes a reference number selected by the user and locates corresponding rows (there can be multiple rows or none to locate) on multiple other sheets by filtering and then copying the data.
This works well except it copies all visible data (columns A-N) when I really want it to copy columns A to K (as L to N on the paste sheet is where I have formulas that set reference numbers and therefore can't be pasted over).
I have tried a couple of changes to the below section of code including offset however it either ignores the offset (probably because I'm using xlCellTypeVisible which I had to do as the data that needs to be copied can be across multiple non sequential rows) or I get an error about selection method not supported.
Any thoughts?
sheet that is being copied - DbExtract
Sheet that data is pasted to - DuplicateRecords
Thanks.
Sub UpdateInputWithExisting()
' Other code that works using set with values and active cell offset with values for other sheets
Sheets("TK_Register").Range("A1").CurrentRegion.AutoFilter field:=12, Criteria1:=RefID
Dim DbExtract, DuplicateRecords As Worksheet
Set DbExtract = ThisWorkbook.Sheets("TK_Register")
Set DuplicateRecords = ThisWorkbook.Sheets("EditEx")
DbExtract.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).copy
DuplicateRecords.Cells(31, 3).PasteSpecial xlPasteValues
On Error Resume Next
Sheets("TK_Register").ShowAllData
On Error GoTo 0
ActiveWorkbook.RefreshAll
Sheets("EditEx").Select
ActiveWindow.SmallScroll Down:=-120
Range("B14:M14").Select
MsgBox ("Record Retrieved. Make your changes and ensure you click 'Save Changes' to update the Master Registers")
End Sub
Need to use .Resize method. Replace this line:
DbExtract.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).copy
With this:
With DbExtract.Range("A1").CurrentRegion
.Resize(, .Columns.Count - 3).SpecialCells(xlCellTypeVisible).Copy
End With
I write a very simple code
Sub copy_unibeton_muss()
Worksheets("source").Range("a8:c8").Copy
End Sub
The code will copy the range A8:C8. I added another portion to paste active cells in the active selected range.
Destination:=ActiveCell
But unfortunately the code paste only the data in the first row onlly. I need to paste data in all selected range may be 5x3 or 6x3 whatever I needed.
Use
Worksheets("source").Range("a8:c8").CopY Destination:= Selection
This will work as expected provided Selection has same number of columns of copied range
As part of my project I have a Table which includes lookup formulas in each column that are dragged down the whole table. Depending on the case only the first x rows return values. I included an iferror so that the lookups that don't return values return "".
Now I want to copy the rows of the table that return values to the first empty row in a different table in a different worksheet.
The code I have so far:
Sub Copy_Results()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet2")
copySheet.Range("Table1").Copy
pasteSheet.ListObjects("Table2").Range.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Now the big problem is that I want to be able to execute this macro multiple times, each time the values from Table 1 should be pasted below the preexisting values in table 2.
The point being that each time the lookup values change meaning I get new results I want to paste them in a table where all the results are documented.
Issues that I had so far:
The first Copy Paste usually works, but when I copy again the values get pasted way below the first ones outside of the table. Usually the full length of the table away. I guess this is because the whole copy table is filled with formulas.
The easiest way to do this is to restrict the cells that you are going to copy using the SpecialCells method:
https://msdn.microsoft.com/en-us/library/office/ff196157.aspx
In this case you only want to copy the formulas that have numbers as the values, so this would be the syntax:
SpecialCells(xlCellTypeFormulas, xlNumbers)
Put into your code it would be:
copySheet.Range("Table1").SpecialCells(xlCellTypeFormulas, xlNumbers).Copy
You can see this in action outside of your code by selecting the complete range in your source sheet then pressing F5, selecting the "Special" button at the bottom of the dialog that pops up, then select "Formulas" and "Numbers".
To make sure it pastes in the next available row, use the CurrentRegion property:
https://msdn.microsoft.com/en-us/library/office/ff196678.aspx?f=255&MSPPError=-2147217396
This code will tell you what the last row is in the used area defined by cell A1:
pasteSheet.cells(1,1).CurrentRegion.Rows.Count
I believe the paste command you're looking for will be close to this (hard to test exactly without your spreadsheet):
pasteSheet.cells(pasteSheet.cells(1,1).CurrentRegion.Rows.Count + 1, 1).PasteSpecial xlPasteValues