I want to copy contents of one row in Excel to other row.
Currently, I am using following code for copying data from previous row.
rngCurrent.Offset(-1).Copy
rngCurrent.PasteSpecial (xlPasteValues)
but I want to skip some columns. So let's say if there are 20 columns, I want to copy all columns except column 4 and 14. How can this be achieved in VBA?
Example:
Assume following is the data in row.
Row to be copied........> 1 2 3 4 5 6 7 8 .... 14 15 16
Target Row Before Copy..> A B C D E F G H .... N O P
Target Row After Copy...> 1 2 3 D 5 6 7 8 .... N 15 16
So everything is copied except column 4 and 14. Note that original values D and N in column 4 and 14 of Target row are preserved.
Another way of doing it.....takes less no. of loops.
Assumptions
1. Skip columns are in ascending order.
2. Skip columns value starts from 1 and not 0.
3. Range("Source") is First cell in source data.
4. Range("Target") is First cell in target data.
How to call :
Thanks.
Try using union of 2 ranges:
Sam
I am not sure exactly how you want to use the macro (i.e. do you select range in sheet, or single cell?) but the following code may get you started:
EDIT - code updated to reflect your comments. I have added a function to check if the columns you want to keep are in the array.
Depending on what you want to do you could augment this code. For example, rather then selecting the range you want to copy, you could click any cell in the row and then use the following to select the
EntireRow
and then perform the copy operation:Hope this helps