简体   繁体   中英

VBA Excel paste changes values

I am copying data from one workbook ("firstWB") to another one ("secondWB"). The first workbook is actually a .txt file. I changed the file extension to .xls to work with it.

In my firstWB I have this column containing ths data:

0
0
0
0
0
2,378
2,378
3,038
3,038
3,146
3,146
3,146
3,151
3,151
3,107
3,107

Using firstWB.Sheets(1).Range(copyRng.Address).Copy I'll put the data into my clipboard. Then I paste it using ActiveSheet.Paste Destination:=Worksheets("Tabelle2").Range("A2") . Copy and pasting works fine, but the values change. In my SecondWB if find this data:

0,000
0,000
0,000
0,000
0,000
2.378
2.378
3.038
3.038
3.146
3.146
3.146
3.151
3.151
3.107

How can I avoid this?

Are the values in firstWB hard data, or is it cell references? I have a feeling that the .copy is copying the cell references, so when you paste, the references then refer to another location with different info.

One possible solution is to set the range values equal to eachother, instead of copy/pasting. This should give you an idea of how to do that (note you will have to tweak it to fit your ranges):

sub test()
dim secondWB as worksheet
dim rng as Range, rng2 as Range 'rng will be the range you want to copy, rng2 is destination

Set rng = Range("A1:A10")
set rng2 = secondWB.range("B1:B10")

rng2.Value = rng.value 'this will set the range values equal.
end sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM