[英]Find and Select cells with specific text in one workbook and copy to another workbook
I'm very new to VBA. I used the "Find & Select" option in Excel to find cells (in column A) containing a certain text, then I used CNTRL A and CNTRL C to select all the matching cells in the column, and finally selected a cell in another workbook where I wanted to copy the selected cells.我是 VBA 的新手。我使用 Excel 中的“查找并选择”选项来查找包含特定文本的单元格(在 A 列中),然后我使用 CNTRL A 和 CNTRL C 到 select 列中所有匹配的单元格,最后在另一个工作簿中选择了一个单元格,我想在其中复制选定的单元格。 Of course it works when done manually but later, when I run the macro on a new sheet, it gets stuck.
当然,它在手动完成时有效,但稍后,当我在新工作表上运行宏时,它会卡住。 Below is what the code looks like - it seems to err at line 4 "ActiveSheet.Paste".
下面是代码的样子——它似乎在第 4 行“ActiveSheet.Paste”处出错。
A few notes:一些注意事项:
Line 4 Error message includes "Runtime error '1004': You can't paste this here because the Copy area and paste area aren't the same size. Select one cell in the paste area (which I think I did when building the macro) or an area that's the same size and try pasting again.第 4 行错误消息包括“运行时错误‘1004’:您不能在此处粘贴,因为复制区域和粘贴区域的大小不同。Select 粘贴区域中的一个单元格(我想我在构建宏时所做的) ) 或大小相同的区域,然后再次尝试粘贴。
When I click "debug" in the pop-up error message box, it highlights Line 4.当我在弹出的错误消息框中单击“调试”时,它突出显示了第 4 行。
Next, see Line 11. I was doing the same function and yet it says "Selection."... with a much longer description of the selection criteria.接下来,请参阅第 11 行。我正在执行相同的 function,但它显示“选择。”...对选择标准的描述更长。 I tried moving Lines 10-15 above line 4, to test what this different coding would do.
我尝试将第 10-15 行移到第 4 行上方,以测试这种不同的编码会做什么。 It failed as well
也失败了
Error message: "Run-time error '91': Object variable or With block variable not set错误消息:“运行时错误‘91’:Object 变量或未设置块变量
Thanks for any help you all can offer!感谢大家提供的任何帮助! Much appreciated!
非常感激!
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
I am at a loss as to what to try to correct the problem(s).我不知道如何尝试纠正问题。
I am sure there is space where I am trying to paste the cells.我确定在我尝试粘贴单元格的地方有空间。 It works very smoothly when I am doing it manually;-) But somehow, the coding isn't picking up what I'm doing.
当我手动执行时,它工作得非常顺利;-) 但不知何故,编码并没有接受我正在做的事情。
I see suggestions we should avoid "Select" in coding but I'm at a loss what to replace it with, especially given I'm not trying to copy a range but rather only cells in that range matching a criteria.我看到了我们应该避免在编码中使用“选择”的建议,但我不知道用什么来替换它,特别是考虑到我不是要复制一个范围,而是只复制该范围内符合条件的单元格。
Two or three things (beside what Ike is recommending you in the comments, recommendation that should be considered):两三件事(除了艾克在评论中向您推荐的内容外,还应考虑的建议):
Here some examples:这里有一些例子:
'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
And finally, you can repeat this code for "602", or just add it to the "if" statement adding more string variables:最后,您可以为“602”重复此代码,或者将其添加到添加更多字符串变量的“if”语句中:
Dim Tex2 as string
Tex2 = "602"
and then...然后...
if InStr(cells(I, 1), Tex) > 0 or InStr(cells(I, 1), Tex2) > 0 then
Let me know if it works让我知道它是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.