Jose M.
Jose M.

Reputation: 2330

checking excel range to see if it is empty

In the code below I am trying to check if cell R23 contains any data, if yes:

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

Upvotes: 1

Views: 8616

Answers (2)

user2480047
user2480047

Reputation:

String.IsNullOrEmpty should do the job:

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

Upvotes: 1

TypeM1smatch
TypeM1smatch

Reputation: 225

Have you tried

If eeName = "" Then ?

Alternatively, try changing eeName to Excel.Range rather than a String, set it equal to your range, then try

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.

Upvotes: 0

Related Questions