2014-09-19 68 views
1

我试图做一个If语句来检查给定的单元格是否存在(有些被合并)并且具有不同值的' - ',一个语句被执行,在这个查询一个案例。 如果条件不具备,不执行查询如果语句来检查宏中的Excel单元格

CellStr = Range("C3").Text 
If CellStr <> "-" Then 
Set rs = conn.Execute("QUERY") 
End If 

目前我的作品当细胞具有价值的代码“ - ”(查询不执行),但是当细胞确实不起作用不存在(查询执行并返回0)

如何保护If语句来解决此问题?

+0

尝试'如果CellStr <>“ - ”和CBool​​(Len(CellStr))然后' – Jeeped 2014-09-19 11:24:26

+0

工作。谢谢 – 2014-09-19 11:28:27

回答

0

检查电池是否存在(即未覆盖合并单元格),你检查单元格中的数值/文字之前

Sub test() 

    Dim c As Range 
    Set c = Range("C3") 'Set the cell 

    'Check if the cell exists, i.e. is not covered by merged cells 
    If c.MergeArea.Cells(1, 1).Address = c.Address Then 

     'Check if the cell text is different from "-" 
     If c.Text <> "-" Then 

      'Execute something 
      MsgBox "Put your statement here" 

     End If 

    End If 

End Sub 

最好的问候,

西蒙