简体   繁体   English

在一个工作簿中查找和 Select 个具有特定文本的单元格并复制到另一个工作簿

[英]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:一些注意事项:

  1. Column A is the column containing data I'm using to find & select from A 列是包含我用来从中查找 & select 的数据的列
  2. L5 is one of the cells I select to paste cells found/selected (trying to paste other selections in I5, o5, o12, o26, o40, and o53 L5 是 I select 的单元格之一,用于粘贴找到/选择的单元格(尝试粘贴 I5、o5、o12、o26、o40 和 o53 中的其他选择
  3. "HS" in Line 11 and later "602" are two of seven search criteria used in "find & select".第 11 行中的“HS”和后面的“602”是“查找和选择”中使用的七个搜索条件中的两个。 Interesting 5 of the search criteria I used don't show up in coding.我使用的有趣的 5 个搜索条件没有出现在编码中。

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):两三件事(除了艾克在评论中向您推荐的内容外,还应考虑的建议):

  • What gives you an error in line 4 is that you are trying to paste an entire column in a space smaller.第 4 行中的错误是您试图将整个列粘贴到较小的空间中。 It would only work if you paste it in the first cell of the column.只有将它粘贴到列的第一个单元格中,它才会起作用。 I'm sure if you delete that row, it will give you the same error in other similar "activesheet.paste".我敢肯定,如果您删除该行,它会在其他类似的“activesheet.paste”中给您带来相同的错误。
  • It's better to stop using the "range" method for referring to only one cell.最好停止使用“范围”方法来仅引用一个单元格。 Some methods only work using "cells(2, 3)" for cell C2, for example.例如,某些方法仅适用于单元格 C2 使用“cells(2, 3)”。 It's easier to use variables as well with this.使用变量也更容易。
  • Instead of using the "find" method, try using a combination of a for loop and an "if statement".不要使用“查找”方法,而是尝试结合使用 for 循环和“if 语句”。

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.

相关问题 将单元格从一个工作簿(非特定)复制到另一个(特定) - Copy cells from one Workbook (non-specific) to another (specific) Excel VBA代码,用于在一个工作簿中选择包含特定文本字符串的单元格,然后将这些单元格复制并粘贴到新工作簿中 - Excel VBA code to select cells that contain a certain text string in one workbook, and then copy and paste these cells into a new workbook VBscript:将数据从一个工作簿复制/粘贴到另一工作簿的下一个空白行(特定单元格) - VBscript: Copy/Paste data from one workbook to next blank row (specific cells) of another workbook 如何使用C#将一个Excel工作簿中特定范围的单元格复制到另一工作簿中 - how to copy cells from a specific range from one excel workbook to another workbook using c# 通过检查单元格从一个工作簿复制到另一个工作簿 - Copy from one workbook to another with checking cells 将单元格范围从一个工作簿复制到另一个工作簿 - Copy range of cells from one workbook to another 从一个工作簿复制到另一个工作簿中的特定工作表 - Copy from one workbook to specific sheet in another workbook 将特定内容从一个excel工作簿复制到另一工作簿 - copy specific content from one excel workbook to another workbook 将特定行从一个工作簿复制到另一个工作簿 - Copy specific Rows from one workbook to another 根据条件将特定单元格从一个工作簿复制到另一个工作簿的不同单元中 - Copy specific cells from one workbook to different cells in another depending on condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM