I am trying to build excel sheets with conditional formatting. I have everything working but on condition. I need it to detect an "OK" in the cell. That works with the following code but it also works with just "O" or even a number for some reason. The Case "OK" section is what is failing. How do i set up the condition to only accept OK?
Code:
Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
Dim rng As String = "E7:ZZ" & (6 + dgRows.RowCount).ToString
With xlWorkSheet.Range(rng)
.FormatConditions.Delete()
.FormatConditions.Add(Excel.XlFormatConditionType.xlBlanksCondition)
.FormatConditions(1).Interior.Color = RGB(244, 199, 195)
.Locked = False
End With
For z = 0 To dgRows.RowCount - 1 Step 1
Dim i As Integer = z + 1
rng = dgRows.Rows(z).Cells(1).Value.ToString
Try
With xlWorkSheet.Range(rng)
Select Case dgRows.Rows(z).Cells(5).Value.ToString
Case "Between"
.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlNotBetween, CInt(dgRows.Rows(z).Cells(6).Value), CInt(dgRows.Rows(z).Cells(7).Value))
.FormatConditions(2).Interior.Color = RGB(244, 199, 195)
.FormatConditions(2).Font.Color = Color.Red
Case "Min"
.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlLess, CInt(dgRows.Rows(z).Cells(6).Value))
.FormatConditions(2).Interior.Color = RGB(244, 199, 195)
.FormatConditions(2).Font.Color = Color.Red
Case "Max"
.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlGreater, CInt(dgRows.Rows(z).Cells(7).Value))
.FormatConditions(2).Interior.Color = RGB(244, 199, 195)
.FormatConditions(2).Font.Color = Color.Red
Case "OK"
.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlNotEqual, "OK")
.FormatConditions(2).Interior.Color = RGB(244, 199, 195)
.FormatConditions(2).Font.Color = Color.Red
Case Else
'Exit Sub
End Select
End With
Catch ex As Exception
MsgBox("error2: " & ex.Message.ToString)
End Try
xlWorkSheet.Range("A" & (7 + z).ToString).Value = dgRows.Rows(z).Cells(2).Value.ToString
xlWorkSheet.Range("B" & (7 + z).ToString).Value = dgRows.Rows(z).Cells(3).Value.ToString
xlWorkSheet.Range("C" & (7 + z).ToString).Value = dgRows.Rows(z).Cells(4).Value.ToString
xlWorkSheet.Range("D" & (7 + z).ToString).Value = dgRows.Rows(z).Cells(5).Value.ToString
Next
xlWorkSheet.Protect(txtPass.Text, True)
If saveIT Then
xlWorkBook.SaveAs(IO.Path.Combine(myFiles, txtFile.Text & ".xlsx"))
saveIT = False
End If
MsgBox("File Generated")
End Sub