vba 1004 错误:未找到单元格,简单的解决方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25380886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:49:26  来源:igfitidea点击:

1004 Error: No cells were found, easy solution?

excelvbadebuggingexcel-vba

提问by Lewis Heslop

In my macro, I have a segment which looks at a range, and finds and fills the blank cells.

在我的宏中,我有一个查看范围并查找并填充空白单元格的段。

Range("E10:A" & CStr(bottom - 1)).Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Value = "N/A"

where

在哪里

bottom = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row

This works perfectly when a blank cell is present within the range, but throws "1004 Error: No cells were found." at the specialcells.select line. I am having trouble of thinking of an easy way to fix this problem.

当范围内存在空白单元格时,这非常有效,但会引发“1004 错误:未找到单元格”。在 specialcells.select 行。我很难想出一个简单的方法来解决这个问题。

I understand that I could run a loop through the range to first check for blank cells, but I feel that this method would be slow and clunky.

我知道我可以在范围内循环运行以首先检查空白单元格,但我觉得这种方法会很慢而且很笨拙。

Does anyone have a quicker and/or easier solution?

有没有人有更快和/或更容易的解决方案?

PS I know that I can consolidate my above lines of code, I just laid it out like this here to be easier to understand;

PS我知道我可以合并我上面的代码行,我只是把它放在这里更容易理解;

    Range("E10:A" & CStr(bottom - 1)).SpecialCells(xlCellTypeBlanks).Value = "N/A"

回答by

What you could do to avoid the error popping up is add an error handler

您可以做些什么来避免错误弹出是添加一个错误处理程序

For Example:

例如:

Sub Main()
    On Error GoTo NoBlanks
    Range("A1:A10").SpecialCells(xlCellTypeBlanks).Value = "N/A"

NoBlanks:
    Resume Next
    ' or add code here to execute when there are no empty cells
End Sub