简体   繁体   中英

Error when reading every line in excel with blank lines

Hi i'm having a problem here. I have a simple excel file and all i want to do is to read all rows in a column and find a specific string. But an error appears, 'Conversion from string "START" to type 'Double' is not valid ERROR', and i google it and found out that it's because of the blank lines. So how could i possibly just ignore the blank lines? So far here's my code:

For Each c In xlsWorkSheet.Range("B1:B300").Cells
   If IsNothing(c.value) Then
       x = x + 1
       MessageBox.Show(x)
   ElseIf c.Value = "START" Then
       MessageBox.Show(x)
   End If
Next

First row is a blank lines so it outputs: 1

Second row has a value and outputs: 2

But when it reads the 3rd row which has no value/ blank then it outputs the error.


So far this is was the code that i've done:

  For Each c In xlsWorkSheet.Range("B1:B300").Cells
                If IsNothing(c.value) Then
                    x = x + 1
                ElseIf Not IsNothing(c.Value) Then
                    x = x + 1
                    If c.value.ToString.ToUpper <> "START" And c.value.ToString.ToUpper <> "END" Then
                    ElseIf c.value.ToString.ToUpper = "START" Then
                        start = x
                    ElseIf c.value.ToString.ToUpper = "END" Then
                        ends = x
                    End If
                End If
            Next

            MessageBox.Show(start)
            MessageBox.Show(ends)

            Dim objAdapter1 As New OleDbDataAdapter("SELECT * FROM [WORK SCHEDULE$B" & start & ":I" & ends & "]", objConn)

            Dim objDataset1 As New DataSet
            objAdapter1.Fill(objDataset1)

            DataGridView1.DataSource = objDataset1.Tables(0).DefaultView

And this code is running successfully :)

Since you want to know what row 'START' is then try the code below..

UPDATED:

try this:

add this code after LastUsedCol

    Dim _findSTART As Excel.Range = Nothing

    _findSTART = xlsWorkSheet.Range("B1:B" & lastUsedRow).Find("START", , _
                                Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _
                                Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)

then update your `objAdapter1`

    Dim objAdapter1 As New OleDbDataAdapter("SELECT * FROM [Sheet1$B" & _findSTART.Row & ":I" & lastUsedRow & "]", objConn)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM