简体   繁体   English

VBA Excel粘贴更改值

[英]VBA Excel paste changes values

I am copying data from one workbook ("firstWB") to another one ("secondWB"). 我正在将数据从一个工作簿(“ firstWB”)复制到另一个工作簿(“ secondWB”)。 The first workbook is actually a .txt file. 第一个工作簿实际上是一个.txt文件。 I changed the file extension to .xls to work with it. 我将文件扩展名更改为.xls以使用它。

In my firstWB I have this column containing ths data: 在我的firstWB中,此列包含此数据:

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. 使用firstWB.Sheets(1).Range(copyRng.Address).Copy将数据放入剪贴板。 Then I paste it using ActiveSheet.Paste Destination:=Worksheets("Tabelle2").Range("A2") . 然后我使用ActiveSheet.Paste Destination:=Worksheets("Tabelle2").Range("A2")粘贴它。 Copy and pasting works fine, but the values change. 复制和粘贴工作正常,但是值会更改。 In my SecondWB if find this data: 在我的SecondWB中,如果找到以下数据:

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? 是firstWB硬数据中的值,还是单元格引用? 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. 我感觉.copy正在复制单元格引用,因此在粘贴时,引用将引用具有不同信息的另一个位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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