gpt4 book ai didi

vba - 剪切、复制和粘贴excel

转载 作者:行者123 更新时间:2023-12-04 20:16:00 29 4
gpt4 key购买 nike

在 VBA 中,您可以在剪切和粘贴后直接进行复制和粘贴。我之所以问,是因为 for 循环中的 If 语句需要剪切一行数据,然后直接在下面复制该行。当我运行代码时,它会执行该剪切,但不会执行副本。我在网上查看了这个,我唯一能想到的就是清除剪贴板。可悲的是,这无济于事:(。

这是我的代码:

im r As Range
Dim i As Long, i2 As Long, i4 As Long
i = 2
i2 = 15
i3 = 70
i4 = 3
For Each r In ActiveSheet.UsedRange
v = r.Value
If InStr(v, "ION") > 0 And InStr(v, "Dog") > 0 Then
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column + 2)).Cut
Range(Cells(i, 8), Cells(i, 10)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range(Cells(r.Row + 1, r.Column), Cells(r.Row + 1, r.Column + 2)).Copy
Range(Cells(i4, 8), Cells(i4, 10)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
i = i + 2
i4 = i4 + 2

然后有更多代码使用 elseif 来搜索其他词,但上面的代码是有问题的代码。

重申一下,剪切/粘贴有效。复制/粘贴没有。

谢谢。

最佳答案

我真的无法复制您遇到的问题,但您可以尝试更改以下三行:

Range(Cells(r.Row + 1, r.Column), Cells(r.Row + 1, r.Column + 2)).Copy
Range(Cells(i4, 8), Cells(i4, 10)).Select
ActiveSheet.Paste

对此:
Range(Cells(r.Row + 1, r.Column), Cells(r.Row + 1, r.Column + 2)).Copy _
Range(Cells(i4, 8), Cells(i4, 10))

你可以用 .Cut 做同样的事情方法:,这将成为:
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column + 2)).Cut _
Range(Cells(i, 8), Cells(i, 10))

注意:很少需要激活, Select或以其他方式“单击”您需要使用的范围对象。这是一个很好的解释:

How to avoid using Select in Excel VBA macros

关于vba - 剪切、复制和粘贴excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873529/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com