我对VBA很陌生。我使用Excel中的"查找和选择"选项来查找单元格(在列A中)包含某个文本,然后我使用CNTRL A和CNTRL C来选择列中所有匹配的单元格,最后在另一个工作簿中选择了一个单元格,我想将所选单元格复制到其中。当然,手动完成时它可以工作,但后来,当我在新工作表上运行宏时,它卡住了。下面是代码的样子-它似乎在第4行"ActiveSheet. Paste"出错。
注意:
1.列A是包含我用来查找和选择的数据的列
- L5是我选择的要粘贴找到/选定单元格的单元格之一(尝试粘贴I5、o5、o12、o26、o40和o53中的其他选定内容
1.第11行中的"HS"和后面的"602"是"find & select"中使用的7个搜索条件中的两个。有趣的是,我使用的5个搜索条件在编码中没有出现。
第4行错误消息包括"运行时错误" 1004 ":无法在此处粘贴,因为复制区域和粘贴区域大小不同。请选择粘贴区域中的一个单元格(我想我在构建宏时已经这样做了)或大小相同的区域,然后重试粘贴。
当我在弹出的错误消息框中单击"debug"时,它突出显示了第4行。
接下来,请看第11行。我正在执行相同的功能,但它显示"Selection"。......选择标准的描述要长得多。我尝试将第10 - 15行移到第4行上方,以测试这种不同的编码会做什么。同样失败
错误信息:"运行时错误'91':未设置对象变量或With块变量
感谢你们所提供的任何帮助!非常感谢!
Columns("A:A").Select.
Selection.Copy
Range("L5").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Copy
Range("I5").Select
ActiveSheet.Paste
Columns("A:A").Select
Selection.Find(What:="HS", After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Application.CutCopyMode = False
Selection.Copy
Range("O5").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Copy
Range("O12").Select
ActiveSheet.Paste
Columns("A:A").Select
Range("A17").Activate
Application.CutCopyMode = False
Columns("A:A").Select
Range("A93").Activate
Selection.Copy
Range("O26").Select
ActiveSheet.Paste
Range("O26:O37").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Selection.Cut
Application.CutCopyMode = False
Selection.ClearContents
Columns("A:A").Select
Range("A21").Activate
Selection.Copy
Range("O40").Select
ActiveSheet.Paste
Selection.Find(What:="602", After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Application.CutCopyMode = False
Selection.ClearContents
Columns("A:A").Select
Range("A28").Activate
Selection.Copy
Range("O53").Select
ActiveSheet.Paste
我不知道该怎么解决这个问题。
我确信我试图粘贴单元格的地方有空间。当我手动操作时,它工作得非常流畅;- )但不知何故,编码并不知道我在做什么。
我看到了一些建议,我们应该在编码中避免"选择",但我不知道用什么来替换它,特别是考虑到我不想复制一个范围,而只是复制该范围中匹配条件的单元格。
1条答案
按热度按时间avwztpqn1#
两到三件事(除了艾克在评论中推荐你,应该考虑的建议):
以下是一些例子:
最后,您可以为"602"重复这段代码,或者只是将其添加到"if"语句中,添加更多字符串变量:
然后。
如果成功了告诉我