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
Related
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.
I am trying to transfer selected data from one worksheet to another via a Transfer macro.
Every time I try to do so, I get the following message in Debug:
Sub Transfer Macro
ActiveCell.Offset(-10, -7).Range("A1").Select
Selection.Copy
Sheets("Data Presentation Template").Select
ActiveCell.Offset(-25, -6).Range("A1").Select
ActiveSheet.Paste
End Sub
Is there a better way to transfer/ mirror the data from a data entry sheet to a presentation sheet with out macro?
Your ActiveCell.Offset(r, c) commands use negative numbers. These will depend on whether the currently active cell is in a position where the negative offsets don't try to .Select a cell that is either off the worksheet to the left of column A or a cell that is above row 1.
ActiveCell.Offset(-10, -7).Range("A1").Select
This demands that the ActiveCell is at least 10 rows down and 7 columns right from A1; specifically K8 or to the right/down of K8. Anything closer to A1 will produce an error since you are trying to select a cell that is off the worksheet.
ActiveCell.Offset(-25, -6).Range("A1").Select
The same for this statement but the minimal distance for the ActiveCell from A1 would be Z7.
The ActiveCell property changes every time you select another cell. It cannot be relied upon to 'automatically transfer' data between worksheets unless you check to ensure that the ActiveCell is where you want it on each worksheet before running your sub procedure.
Any further recommendations would require specific worksheet names and ranges for the source and target of the copy & paste operation.
The .Range("A1") in ActiveCell.Offset(-10, -7).Range("A1").Select simply means the top-left cell in the ActiveCell.Offset(-10, -7) range. It is likely a 'hangover' from relative positioned macro recording.
I'm using a VBA routine to copy
Source sheet: named range (multiple 'selections')
Target sheet: copy-paste formula from cells in named range, to cells with a certain offset from a cell on the target sheet (this offset is depending on a certain selection).
I have two types of named ranges on the source sheet; one from which I only want to copy the values (=rng_operationeel_input_data), one from which I would like to copy to formulas (=rng_operationeel_formules). The formulas should be copied 'relatively', to have references on the target sheet (which are part of the copied values from the other range). For that reason, I can't use "targetCell.Formula = sourceCell.Formula", as it then literally copies the absolute formula. Not relative.
That's why I'm using sourceCell.
I do this in a for each loop over all cells in the source range, as the named range is not one single range (set of ranges).
Note. 'datasetReferenceCell' is the cell on the target sheet from which the offset is taken for pasting.
The problem is that, even if I use VBA to do the copy-pasting, without using 'select' somewhere, still at the end the user is confronted with the target sheet. (Excel will move to the target sheet)
This is only happening, for the copy-paste part.
How can I prevent this from happening?
edit: note that I am already using "Application.ScreenUpdating" (at start to false, at the end to true). I also have a MsgBox at the end of the routine (for info that routine was successful). Excel is moving to the target worksheet after the MsgBox is closed.
Below the VBA code part.
' dataset for weeknr found: save data to dataset
Dim dataRange As Range, dataField As Range
' for each cell in input data range: save value in dataset
Set dataRange = Range("rng_operationeel_input_data")
For Each dataField In dataRange
datasetReferenceCell.Offset(dataField.Row, dataField.Column).Value = dataField.Value
Next dataField
' !!! Following are only saved, not loaded, as it are formula based fields
' for each cell in formula range: paste formula
Set dataRange = Range("rng_operationeel_formules")
For Each dataField In dataRange
dataField.Copy
datasetReferenceCell.Offset(dataField.Row, dataField.Column).PasteSpecial (xlPasteFormulas)
'datasetReferenceCell.Offset(dataField.Row, dataField.Column).Formula = dataField.Formula 'not working, as relative formulas are required
Next dataField
The only way I was able to solve it was to reset the active sheet:
Public Sub routine()
Dim activeWs As Worksheet
Set activeWs = ThisWorkbook.ActiveSheet
.. code including copy & PasteSpecial ...
activeWs.Activate
End Sub
If I work in my spreadsheed, without using any vba-code, I could select a single cell, copy, then select a bunch of cells, and paste. The result would be that all the target cells are filled with whatever value was in the source cell.
How would I do this in vba-code?
You would think the following would work because it mimics the behavior described above, but it doesn't. It will fill the top cell of the target range with the value and leave the rest of the cells empty.
Range("A1").Select
Selection.Copy
Range("B1:B12").Select
ActiveSheet.Paste
You don't need to use copy & paste in VBA, you can set values on ranges, like so:
Range("B1:B12").Value = Range("A1").Value
Mark selected cells.
Write the text your want to copy.
Hit Ctrl + Enter On Windows or control + return on Mac.
Magic!
I have a button driven macro that copies a range of cells to the clipboard (so I can place in other documents):
Sub Button2_Click()
'
' Button2_Click Macro
Range("A1:p43").Copy
End Sub
Form time to time I need to either add or delete a row in that range in the worksheet. What I need is for the copy range to expand (or contract) to the number of rows containing the values. Note: the columns will not adjust, just the rows.
If the copy range is a contiguous range of values with a blank column in column Q and a blank row below row 43, you can use the CurrentRegion property like this:
Range("A1").CurrentRegion.Copy
It's basically the same as clicking A1 and then using Ctrl-Shift-Down arrow and Ctrl-Shift-Right arrow.