Microsoft KB Archive/113703

= More Data Pasted Than Expected with SELECTION.OFFSET.SELECT =

Article ID: 113703

Article Last Modified on 8/15/2003

-

APPLIES TO


 * Microsoft Excel 95a
 * Microsoft Excel 5.0c

-



This article was previously published under Q113703





SYMPTOMS
When you use the following Visual Basic, Applications Edition, command in a macro SELECTION.OFFSET(rowOffset, columnOffset).SELECT in conjunction with the Copy and Paste methods, you may receive the following unexpected results:
 * More data is pasted to the destination than expected. -or-


 * You receive the "Copy and Paste Areas Are Different Shapes" error message.



CAUSE
When you use a macro to paste data in a worksheet, and you then attempt to paste another selection to the same worksheet, if you do not make a new selection for the range that you want to paste to, the copied data is pasted to the range that you previously pasted to. For example, if you copy one column of cells, and the last range you pasted to contained three columns, when you use the Paste method without making another selection, the three columns are filled with the single column of data you copied.

If you use the Offset method to offset the selected range, the offset selection retains the size of the previously pasted range (that is, the entire selected range is offset by the number of specified rows and columns).

In addition, because the Paste method allows you to paste data to fill the entire selected range, when you offset the selected range, if that range is larger than the range of data you want to paste, Microsoft Excel will fill the remaining cells in the selection.



WORKAROUND
To work around this problem, create a variable for your selection, and use the Resize method to make the destination range the desired size. For example, to create a destination range that is offset by the number of columns in your selection, but is resized to one column, use the following code: x = Selection.Columns.Count Selection.Offset(0, x).Resize(, 1).Select



Steps to Reproduce Behavior
 Create a workbook with two worksheets, Sheet2 (the sheet that the original data will be on), and Sheet 1 (the sheet on which data will be pasted).  On Sheet2, type the following information:           A        B      C      -

1   100      200    300      2    100      200    300      3    100      200    300      4    100      200    300      5    100      200    300   In a Visual Basic module, type the following macro: Sub Test ' Select the first two columns from the source sheet Sheets("Sheet2").Select Range("A1:B5").Select Selection.Copy ' Paste the data into the destination sheet Sheets("Sheet1").Select ActiveSheet.Paste Application.CutCopyMode = False ' Copy the third column from the destination sheet Sheets("Sheet2").Select Range("C1:C5").Select ' Paste it into the destination area Selection.Copy Sheets("Sheet1").Select Selection.Offset(0,2).Select ActiveSheet.Paste End Sub 

When you run this macro, column C is pasted to fill the selected range (in this case, the selected range is C1:D5 which is the result of offsetting the originally selected range, A1:B5, by two columns).           A        B      C      D      - 1   100      200    300    300      2    100      200    300    300      3    100      200    300    300      4    100      200    300    300      5    100      200    300    300 To edit this macro so that the data is copied correctly, replace the line of code that reads: Selection.Offset(0,2).Select

-with-

x = Selection.Columns.Count Selection.Offset(0, x).Resize(, 1).Select When you run the macro with this change included, the copied data is pasted once into the correct paste range.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft Support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Additional query words: 5.00c 7.00a XL

Keywords: kberrmsg KB113703

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.