Pasting copied columns in VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've written some VBA code which selects then deletes certain columns.

I then want to cut two columns and paste them at the end of the data.

I've written the code below, but Excel doesn't like the last line, where I've selected column U and attempted to paste the columns I'd just cut (columns H and I).

Any ideas why?

TIA


Sub ColumnChanges


Range("C:D,F:J,AB:AB").Select
Selection.Delete
Range("H:I").Cut
Range("U:U").PasteSpecial.Values


End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
In what way does excel not like the last line, i.e. what is the error message?
 
Upvote 0
It won't like it because you are cutting 2 columns and trying to paste to one.

Try changing

Code:
Range("U:U").PasteSpecial.Values
to
Code:
Range("U1").PasteSpecial xlValues
 
Upvote 0
Alternatively, use copy instead of cut.

Code:
Sub ColumnChanges()
    Range("C:D,F:J,AB:AB").Select
    Selection.Delete
    Range("H:I").Copy
    Range("U:U").PasteSpecial (xlPasteValues)
    Columns("H:I").Clear
End Sub
 
Upvote 0
I think that you will find that you still need to use either a single cell or 2 columns (by the way I don't think my last code will work but can't test on my phone)...

Code:
Sub ColumnChanges()
    Range("C:D,F:J,AB:AB").Select
    Selection.Delete
    Range("H:I").Copy
    Range("[COLOR="#FF8C00"]U1[/COLOR]").PasteSpecial (xlPasteValues)
    Columns("H:I").Clear
End Sub
 
Upvote 0
Hi rlv01

Thanks for the prompt response - the error message is "Run-time error '1004': Unable to get the PasteSpecial property of the Range Class.
 
Upvote 0
Hi Mark, thanks for the prompt response.

I tried that (please see below), but it still gave me an error message saying "Run-time error '1004': Unable to get the PasteSpecial property of the Range Class." It's a good idea, though. It just seems to be a strange problem! Any other ideas, please?

Sub ColumnChanges()


Range("C:D,F:J,AB:AB").Select
Selection.Delete
Range("H:I").Cut
Range("U1").PasteSpecial.xlValues




End Sub
 
Upvote 0
Ok, thanks rlv01 - this code worked! Thank you! Can you please remind me how to paste the columns as they are, rather than pasting as "values" only?
 
Upvote 0
Thanks Mark - this worked! But can you please remind me how to paste normally rather than specially in VBA? When I typed "Paste..." after selecting cell U1 in my original code, the default option was to "paste special" but I want to actually just paste the columns that have been cut.
 
Upvote 0
Can you please remind me how to paste the columns as they are, rather than pasting as "values" only?
Code:
Sub ColumnChanges()
    Range("C:D,F:J,AB:AB").Select
    Selection.Delete
    Range("H:I").Copy
    Range("U:U").PasteSpecial (xlPasteAll)
    Columns("H:I").Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top