vb.net 检查excel范围以查看它是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17509009/
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
checking excel range to see if it is empty
提问by Jose M.
In the code below I am trying to check if cell R23 contains any data, if yes:
在下面的代码中,我试图检查单元格 R23 是否包含任何数据,如果是:
perform action
执行动作
if not
如果不
perform other action.
执行其他操作。
The problem is that the cell is empty, but contains a formula which may return a value or not. Hence, why I am checking for values. The problem is that my code is looking at the formula in the cell and thinks is a string for some reason. If I remove the formula, then my code executes as planned.
问题是单元格是空的,但包含一个可能返回或不返回值的公式。因此,为什么我要检查值。问题是我的代码正在查看单元格中的公式,并且由于某种原因认为是一个字符串。如果我删除公式,那么我的代码会按计划执行。
I can't think of what I am doing wrong here?
我想不出我在这里做错了什么?
Option Explicit On
Option Strict On
Private Sub radMoveToRowQ23EE_MouseHover(sender As Object, e As EventArgs) Handles radMoveToRowQ23EE.MouseHover
Dim eeName As String
Dim WB As Excel.Workbook
Dim WS as Excel.Worksheet
WB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
WS = CType(WB.Worksheets("positionBoard"), Excel.Worksheet)
eeName = CStr(WS.Range("R23").Value)
If eeName Is Nothing Then
Me.tipSelectEmploye.SetToolTip(Me.radMoveToRowQ23EE, "No employee details to display")
Else
Me.tipSelectEmploye.SetToolTip(Me.radMoveToRowQ23EE, "Display details for employee: " & eeName)
End If
End Sub
采纳答案by varocarbas
In VBA, this is accomplished by relying on the function IsEmpty. Thus in VB.NET String.IsNullOrEmptyis the best equivalent:
在 VBA 中,这是通过依赖函数来实现的IsEmpty。因此在 VB.NET 中 String.IsNullOrEmpty是最好的等价物:
If (String.IsNullOrEmpty(eeName)) Then
Me.tipSelectEmploye.SetToolTip(Me.radMoveToRowQ23EE, "No employee details to display")
Else
Me.tipSelectEmploye.SetToolTip(Me.radMoveToRowQ23EE, "Display details for employee: " & eeName)
End If
回答by TypeM1smatch
Have you tried
你有没有尝试过
If eeName = "" Then?
If eeName = "" Then?
Alternatively, try changing eeName to Excel.Range rather than a String, set it equal to your range, then try
或者,尝试将 eeName 更改为 Excel.Range 而不是 String,将其设置为等于您的范围,然后尝试
If eeName.value = "" Then
If eeName.value = "" Then
You may also want to look at .text rather than .value. Although, .value is the default property for Excel ranges. Sorry I can't be a little more conclusive, but these methods have worked in my experience.
您可能还想查看 .text 而不是 .value。尽管 .value 是 Excel 范围的默认属性。对不起,我不能再确定了,但这些方法在我的经验中是有效的。

