Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Sue Sue is offline
external usenet poster
 
Posts: 285
Default Selecting columns to copy and paste

Help! This code is meant to insert 2 columns to the right, copy the last two
used columns and place their formulae in the new columns. It has been
recorded with actions which do just that, but this macro does not work. It
works to the * but at that point selects all the columns in the work sheet
not just the last 2. This is a disaster and I can not seem to stop it doing
that. I suspect the line two below would have the same issue.

Range("a2").Select 'data headings in this row
Selection.End(xlToRight).Select 'gets to end column via data headings
ActiveCell.Offset(0, 1).Range("a1:b1").Select 'selection of 2 columns at
end
Selection.EntireColumn.Insert 'puts new columns in
* ActiveCell.Offset(0, -2).Columns("A:B").EntireColumn.Select 'stuffs up
here!
Selection.Copy
ActiveCell.Offset(0, 2).Columns("A:B").EntireColumn.Select
Selection.PasteSpecial Paste:=xlFormulas
Application.CutCopyMode = False
Calculate

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Selecting columns to copy and paste

Works OK for me in 2003. Have you tried shutting down Excel and restarting
it? That sometime works for me when Excel macros are misbehaving.

"Sue" wrote:

Help! This code is meant to insert 2 columns to the right, copy the last two
used columns and place their formulae in the new columns. It has been
recorded with actions which do just that, but this macro does not work. It
works to the * but at that point selects all the columns in the work sheet
not just the last 2. This is a disaster and I can not seem to stop it doing
that. I suspect the line two below would have the same issue.

Range("a2").Select 'data headings in this row
Selection.End(xlToRight).Select 'gets to end column via data headings
ActiveCell.Offset(0, 1).Range("a1:b1").Select 'selection of 2 columns at
end
Selection.EntireColumn.Insert 'puts new columns in
* ActiveCell.Offset(0, -2).Columns("A:B").EntireColumn.Select 'stuffs up
here!
Selection.Copy
ActiveCell.Offset(0, 2).Columns("A:B").EntireColumn.Select
Selection.PasteSpecial Paste:=xlFormulas
Application.CutCopyMode = False
Calculate

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Selecting columns to copy and paste

I like to start at the far right and come back looking for the last used column.

If that's ok with you, you may want to try this:

Option Explicit
Sub testme()
Dim LastCol As Long
With ActiveSheet
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column

.Cells(1, LastCol - 1).Resize(1, 2).EntireColumn.Copy _
Destination:=.Cells(1, LastCol + 1)
End With
End Sub

I actually copy|pasted everything. If you wanted just the formulas, change it
to:

Option Explicit
Sub testme2()
Dim LastCol As Long
With ActiveSheet
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column

.Cells(1, LastCol - 1).Resize(1, 2).EntireColumn.Copy
.Cells(1, LastCol + 1).PasteSpecial Paste:=xlPasteFormulas
End With
End Sub


Sue wrote:

Help! This code is meant to insert 2 columns to the right, copy the last two
used columns and place their formulae in the new columns. It has been
recorded with actions which do just that, but this macro does not work. It
works to the * but at that point selects all the columns in the work sheet
not just the last 2. This is a disaster and I can not seem to stop it doing
that. I suspect the line two below would have the same issue.

Range("a2").Select 'data headings in this row
Selection.End(xlToRight).Select 'gets to end column via data headings
ActiveCell.Offset(0, 1).Range("a1:b1").Select 'selection of 2 columns at
end
Selection.EntireColumn.Insert 'puts new columns in
* ActiveCell.Offset(0, -2).Columns("A:B").EntireColumn.Select 'stuffs up
here!
Selection.Copy
ActiveCell.Offset(0, 2).Columns("A:B").EntireColumn.Select
Selection.PasteSpecial Paste:=xlFormulas
Application.CutCopyMode = False
Calculate


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
De selecting a range after a copy and paste is performed ML0940 Excel Programming 3 July 21st 08 01:38 AM
Copy paste without selecting sheet or range Richard Excel Programming 2 May 23rd 08 12:22 PM
Selecting data to copy and paste Black country boy Excel Programming 6 April 24th 07 11:26 PM
Auto copy/ paste on relevant work sheet when selecting list item Niraj Manglam Excel Programming 1 June 6th 06 03:13 PM
Copy and paste columns EstherJ Excel Programming 4 April 19th 06 02:05 PM


All times are GMT +1. The time now is 05:05 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"