[英]Find and Select cells with specific text in one workbook and copy to another workbook
我是 VBA 的新手。我使用 Excel 中的“查找并选择”选项来查找包含特定文本的单元格(在 A 列中),然后我使用 CNTRL A 和 CNTRL C 到 select 列中所有匹配的单元格,最后在另一个工作簿中选择了一个单元格,我想在其中复制选定的单元格。 当然,它在手动完成时有效,但稍后,当我在新工作表上运行宏时,它会卡住。 下面是代码的样子——它似乎在第 4 行“ActiveSheet.Paste”处出错。
一些注意事项:
第 4 行错误消息包括“运行时错误‘1004’:您不能在此处粘贴,因为复制区域和粘贴区域的大小不同。Select 粘贴区域中的一个单元格(我想我在构建宏时所做的) ) 或大小相同的区域,然后再次尝试粘贴。
当我在弹出的错误消息框中单击“调试”时,它突出显示了第 4 行。
接下来,请参阅第 11 行。我正在执行相同的 function,但它显示“选择。”...对选择标准的描述更长。 我尝试将第 10-15 行移到第 4 行上方,以测试这种不同的编码会做什么。 也失败了
错误消息:“运行时错误‘91’:Object 变量或未设置块变量
感谢大家提供的任何帮助! 非常感激!
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
我不知道如何尝试纠正问题。
我确定在我尝试粘贴单元格的地方有空间。 当我手动执行时,它工作得非常顺利;-) 但不知何故,编码并没有接受我正在做的事情。
我看到了我们应该避免在编码中使用“选择”的建议,但我不知道用什么来替换它,特别是考虑到我不是要复制一个范围,而是只复制该范围内符合条件的单元格。
两三件事(除了艾克在评论中向您推荐的内容外,还应考虑的建议):
这里有一些例子:
'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
让我知道它是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.