excel 为什么我的CRFIF语句会抛出类型不匹配错误

xmq68pz9  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

我有一个正在为项目构建的函数,其中一个if语句不断抛出类型不匹配错误。下面是相关代码:

Dim conn As ADODB.Connection
Set conn = New ADODB.Connection

With conn
  .ConnectionString = "DRIVER={SQL Server};SERVER=SERVER;DATABASE=DB;Trust_Connection=yes;" ' connection to server
End With
    
conn.Open

Dim year_goal As ADODB.Recordset

sls_rep_wb.Worksheets(yy & " MO-MO").Activate
           
Set year_goal = conn.Execute("SOME SQL QUERY THAT RETURNS A SUM NUMERIC")
            
            
If IsEmpty(Range("AY3")) & year_goal.RecordCount > 0 Then ' this line throws the error
    Range("AY3:" & Range("AY3").Offset(0, 12).Address).Value = (year_goal!Sum) / 12
End If

字符串
首先,我得到一个错误,表明其中一个迭代返回了一个空的year_goal记录集,这就是为什么我添加了记录计数条件。我试图使用BOF和BOF属性来检查一个空的记录集,他们也给出了同样的错误。即使记录集不是空的,这段代码也会抛出一个错误。不知道是什么问题,请帮助。谢谢!

d6kp6zgx

d6kp6zgx1#

运算符“&”仅用于字符串。
下面的句子:
如果IsEmpty(Range(“AY3”))& year_goal.RecordCount > 0则
它应该这样写:
如果IsEmpty(Range(“AY3”))* 且 * year_goal.RecordCount > 0则
参考:https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/and-operator
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/ampersand-operator

相关问题