VSTO - Testing for empty cell not working.

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello folks:

I am using a windows form in VSTO. On the form my code is to check if the cell J34 is empty, if it is give me a message Else perform some code. Right now, if the cell contains a value the Else part of my code works just fine. But if it does not contain a value (which is what I am checking for) it errors out on this line:

Code:
decAnnualMid = Convert.ToDecimal(WS.Application.Range("J34").Value)

I get an error: Format exception was unhandled by user code

I could always use an exception handling as a work around but it does not seem as the right thing to do. Here is the rest of my code

Code:
Option Explicit On
Option Strict On
Imports XL = Microsoft.Office.Tools.Excel




Public Class frmCustomRanges


    Dim thisWB As Excel.Workbook = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)


    'Variables for the MidPoint TextBoxes
    Dim decAnnualMid As Decimal
    Dim decHourlyMid As Decimal

Code:
    Private Sub chk10thPercentile_CheckedChanged(sender As Object, e As EventArgs) Handles chk10thPercentile.CheckedChanged

        'This event runs when the 10th checkbox is checked
        'The event clears the cells where the value will be pasted
        'enters the value from cell J34 into calculationShet H45 and clears
        'the checkboxes for 25th, 50th, 75th, 90th and Other Amount
        'checkboxes.


        Dim decAnnualMid As Decimal


        Dim WS As Excel.Worksheet = CType(CType(thisWB.ActiveSheet, Excel.Worksheet), Excel.Worksheet)


        With WS


            If chk10thPercentile.Checked = True And WS.Application.Range("J34").Value Is Nothing Then


                MsgBox("The Market Data section of the Position Dashboard does not have a value for the 10th Percentile.", MsgBoxStyle.Critical, "Input Error")


                Me.Close()


            Else


                'convert the value of J34 to decimal 
[COLOR=#b22222]                decAnnualMid = Convert.ToDecimal(WS.Application.Range("J34").Value)  'This is my line with the trouble.[/COLOR]


                'convert annual salary to hourly
                decHourlyMid = decAnnualMid / 52 / 40


                'display in the Mid label box
                lblAnnualMid.Text = decAnnualMid.ToString("C")
                lblHourlyMid.Text = decHourlyMid.ToString("C")


                'Uncheck all other boxes


                chk25thPercentile.Checked = False
                chk50thPercentile.Checked = False
                chk75thPercentile.Checked = False
                chk90thPercentile.Checked = False
                chkOtherAmount.Checked = False


            End If


        End With


    End Sub

Any help will be appreciated. Or if anyone knows how to do it differently or better.

Thank you.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top