VBA SQL IIF 喜欢
VBA SQL IIF LIKE
我正在尝试在“备注 1”列中搜索关键字,return 如果找到一个字符串,如果找不到另一个字符串。
我有以下代码可以正常工作,但无法使 IIF 正常工作:
"IIF([Remarks 1] Like '%Pizza%','Pizza','Other') AS [Desc2], " _
(稍后需要对这一栏进行分组)
另外:我可以嵌套IFF来搜索其他关键字吗?
错误:您试图执行的查询不包括指定表达式 'iif([Remarks 1] ALike '%Pizza%','Pizza','Other') 作为一部分聚合函数
Sub MacroTest()
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb = ActiveWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
ws2.Cells.ClearContents
ws2.Range("A1").Value = "Account Name"
ws2.Range("B1").Value = "Account Number"
ws2.Range("C1").Value = "Currency"
ws2.Range("D1").Value = "Description"
ws2.Range("E1").Value = "Credit Amount"
ws2.Range("F1").Value = "Debit Amount"
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\username\Desktop\Test1.xlsx;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
objRecordset.Open "SELECT [Account Name], " _
& "[Account Number], " _
& "[Currency], " _
& "[Description], " _
& "SUM([Credit Amount])," _
& "-SUM([Debit Amount])," _
& "IIF([Remarks 1] Like '%Pizza%','Pizza','Other') AS [Desc2] " _
& "FROM [Sheet1$] " _
& "GROUP BY [Account Name], " _
& "[Account Number], " _
& "[Currency]," _
& "[Desc2]," _
& "[Description]", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
ws2.Range("A2").CopyFromRecordset objRecordset
End Sub
您正在尝试使用不属于聚合的表达式。你能不能试试这样:
objRecordset.Open "SELECT [Account Name], " _
& "[Account Number], " _
& "[Currency], " _
& "[Description], " _
& "SUM([Credit Amount])," _
& "-SUM([Debit Amount])," _
& "[Desc2] " _
& "FROM (" & _
"SELECT [Account Name], [Account Number], [Currency], [Description], [Credit Amount]," & _
" [Debit Amount], IIF([Remarks 1] Like '%Pizza%','Pizza','Other') AS [Desc2]" & _
" FROM [Sheet1$] ) tmp" _
& "GROUP BY [Account Name], " _
& "[Account Number], " _
& "[Currency]," _
& "[Desc2]," _
& "[Description];"
PS:可能此时使用 ACE.Oledb 驱动程序比使用 Jet 更好。
我正在尝试在“备注 1”列中搜索关键字,return 如果找到一个字符串,如果找不到另一个字符串。
我有以下代码可以正常工作,但无法使 IIF 正常工作:
"IIF([Remarks 1] Like '%Pizza%','Pizza','Other') AS [Desc2], " _
(稍后需要对这一栏进行分组)
另外:我可以嵌套IFF来搜索其他关键字吗?
错误:您试图执行的查询不包括指定表达式 'iif([Remarks 1] ALike '%Pizza%','Pizza','Other') 作为一部分聚合函数
Sub MacroTest()
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb = ActiveWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
ws2.Cells.ClearContents
ws2.Range("A1").Value = "Account Name"
ws2.Range("B1").Value = "Account Number"
ws2.Range("C1").Value = "Currency"
ws2.Range("D1").Value = "Description"
ws2.Range("E1").Value = "Credit Amount"
ws2.Range("F1").Value = "Debit Amount"
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\username\Desktop\Test1.xlsx;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
objRecordset.Open "SELECT [Account Name], " _
& "[Account Number], " _
& "[Currency], " _
& "[Description], " _
& "SUM([Credit Amount])," _
& "-SUM([Debit Amount])," _
& "IIF([Remarks 1] Like '%Pizza%','Pizza','Other') AS [Desc2] " _
& "FROM [Sheet1$] " _
& "GROUP BY [Account Name], " _
& "[Account Number], " _
& "[Currency]," _
& "[Desc2]," _
& "[Description]", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
ws2.Range("A2").CopyFromRecordset objRecordset
End Sub
您正在尝试使用不属于聚合的表达式。你能不能试试这样:
objRecordset.Open "SELECT [Account Name], " _
& "[Account Number], " _
& "[Currency], " _
& "[Description], " _
& "SUM([Credit Amount])," _
& "-SUM([Debit Amount])," _
& "[Desc2] " _
& "FROM (" & _
"SELECT [Account Name], [Account Number], [Currency], [Description], [Credit Amount]," & _
" [Debit Amount], IIF([Remarks 1] Like '%Pizza%','Pizza','Other') AS [Desc2]" & _
" FROM [Sheet1$] ) tmp" _
& "GROUP BY [Account Name], " _
& "[Account Number], " _
& "[Currency]," _
& "[Desc2]," _
& "[Description];"
PS:可能此时使用 ACE.Oledb 驱动程序比使用 Jet 更好。