More Data Pasted Than Expected with SELECTION.OFFSET.SELECT

Last reviewed: September 2, 1997
Article ID: Q113703

The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0, 7.0a

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

MORE INFORMATION

Steps to Reproduce Behavior

  1. 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).

  2. 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
    
    

  3. 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 engineers 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.00 5.00c 7.00 7.00a
Keywords : kbprg PgmHowTo PgmOthr kbprg
Version : 5.00 5.00c 7.00 7.00a
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.