More Data Pasted Than Expected with SELECTION.OFFSET.SELECTLast reviewed: September 2, 1997Article ID: Q113703 |
The information in this article applies to:
SYMPTOMSWhen you use the following Visual Basic, Applications Edition, command in a macro
SELECTION.OFFSET(rowOffset, columnOffset).SELECTin conjunction with the Copy and Paste methods, you may receive the following unexpected results:
CAUSEWhen 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.
WORKAROUNDTo 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
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 300To 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).SelectWhen 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |