vba 范围参考是“无”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24689368/
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-12 03:47:14  来源:igfitidea点击:

Range reference is "Nothing"?

excelvbareferencenothing

提问by CheeseMo

I'm trying to write a function to return the next blank row on a sheet to insert input.

我正在尝试编写一个函数来返回工作表上的下一个空白行以插入输入。

The code is at work so I can only summarize.

代码正在运行,所以我只能总结一下。

I do something like Set rng = Range("Data!A" & row)then return the reference.

我做类似的事情Set rng = Range("Data!A" & row)然后返回参考。

Through debugging, I determined the range being created is referencing a valid cell location. However, it is returning "Nothing" instead of an empty Range object. I've gone so far as to set that cell's value to " " (a space) prior to returning the Range and setting it back to "" after.

通过调试,我确定正在创建的范围引用了有效的单元格位置。但是,它返回“Nothing”而不是空的 Range 对象。我已经在返回范围之前将该单元格的值设置为“”(一个空格),然后将其设置回“”。

My question is a two-parter:
(1) why is an empty (cell values) Range reference always returning "Nothing"?
(2) how do I properly reference an empty cell as a valid Range object?

我的问题是一个两部分:
(1)为什么空(单元格值)范围引用总是返回“Nothing”?
(2) 如何正确引用一个空单元格作为有效的 Range 对象?

回答by Gary's Student

Say we have a range and want to Selectthe first genuine empty cell in that range

假设我们有一个范围并且想要选择该范围内的第一个真正的空单元格

Sub FirstBlank(r2 As Range)
    Dim r1 As Range
    Set r1 = Cells.SpecialCells(xlCellTypeBlanks)
    If Intersect(r1, r2) Is Nothing Then
        MsgBox "there is nothing to find"
    Else
        r2.Cells.SpecialCells(xlCellTypeBlanks).Cells(1).Select
    End If
End Sub

回答by Conor

You have a mistake in the code.

您在代码中有错误。

"A"& row = "A 300" (where row=300)

"A"& row = "A 300"(其中 row=300)

VBA inserts the leading space.

VBA 插入前导空格。