excel 在一个工作簿中查找并选择包含特定文本的单元格,然后复制到另一个工作簿

mrphzbgm  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(135)

我对VBA很陌生。我使用Excel中的"查找和选择"选项来查找单元格(在列A中)包含某个文本,然后我使用CNTRL A和CNTRL C来选择列中所有匹配的单元格,最后在另一个工作簿中选择了一个单元格,我想将所选单元格复制到其中。当然,手动完成时它可以工作,但后来,当我在新工作表上运行宏时,它卡住了。下面是代码的样子-它似乎在第4行"ActiveSheet. Paste"出错。
注意:
1.列A是包含我用来查找和选择的数据的列

  1. 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

我不知道该怎么解决这个问题。
我确信我试图粘贴单元格的地方有空间。当我手动操作时,它工作得非常流畅;- )但不知何故,编码并不知道我在做什么。
我看到了一些建议,我们应该在编码中避免"选择",但我不知道用什么来替换它,特别是考虑到我不想复制一个范围,而只是复制该范围中匹配条件的单元格。

avwztpqn

avwztpqn1#

两到三件事(除了艾克在评论中推荐你,应该考虑的建议):

  • 第4行的错误是你试图粘贴一整列到一个更小的空间里。只有当你粘贴到列的第一个单元格里时,它才会起作用。我敢肯定,如果你删除了那一行,它会在其他类似的"activesheet.paste"中给你同样的错误。
  • 最好停止使用只引用一个单元格的"range"方法。例如,一些方法只对单元格C2使用"cell(2,3)"。使用变量也更容易。
  • 不要使用"find"方法,尝试使用for循环和"if语句"的组合。

以下是一些例子:

'For declaring variables
Dim I as long
Dim Ro as long
Dim Tex as string
'For finding the last cell with content in column A
Ro = Cells(Rows.Count, 1).End(xlUp).Row
'This declares the text you are looking in the cells value
Tex = "HS"
'Now the loop, that will go from I = whatever value you want to the value limit you give, which in this case will be the last row, "Ro"
For I = 1 to Ro
  'Here the "if" conditional, that will do something as long as the condition is met
  if InStr(cells(I, 1), Tex) > 0 then 'InStr is a method that gives you the position of a text in the value of a cell, resulting in 0 if can't be found in said value (in this case, the cell in column A and every row from 1 to I)
    Cells(I, 12) = cells(I, 1)
  end if
next I

最后,您可以为"602"重复这段代码,或者只是将其添加到"if"语句中,添加更多字符串变量:

Dim Tex2 as string
Tex2 = "602"

然后。

if InStr(cells(I, 1), Tex) > 0 or InStr(cells(I, 1), Tex2) > 0 then

如果成功了告诉我

相关问题