VBA Addod:从单元格中获取非空值
VBA Addod: take non empty value form the cell
我想从 C6 中获取我的 sql 查询的值。但是,如果 C6 不为空,我想这样做。我不知道如何在我的代码中翻译这个选项。我的代码是:
Public Sub INFO_PROTO34(ByRef strQ As String)
Dim RECSET As New ADODB.Recordset
RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.no_police = Range("C6") and sousc.cd_dossier = 'SOUSC' and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
If Not RECSET.EOF Then
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = RECSET.Fields("b_perf_ctrat_gar").Value
Else
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = "0"
End If
RECSET.Close
End Sub
非常感谢您的建议
在RECSET.OPEN
之前你可以尝试使用IsEmpty
方法
我还没有在 VBA 脚本上对此进行测试,但这里是我如何更改您的代码以根据其是否为空白来考虑 C6 单元格:
Public Sub INFO_PROTO34(ByRef strQ As String)
Dim RECSET As New ADODB.Recordset
if IsEmpty(Range("C6").value) = true then
' if C6 cell is blank or empty it will do the SQL without the C6 cell value
RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.cd_dossier = 'SOUSC' and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
Else
' in this case the cell C6 is not empty/blank so it will use your existing SQL statement
RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.no_police = Range("C6") and sousc.cd_dossier = 'SOUSC' and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
end if
If Not RECSET.EOF Then
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = RECSET.Fields("b_perf_ctrat_gar").Value
Else
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = "0"
End If
RECSET.Close
End Sub
有关 IsEmpty
方法的详细信息,请参阅 https://www.techonthenet.com/excel/formulas/isempty.php。
例如:
Public Sub INFO_PROTO34(ByRef strQ As String)
Dim RECSET As New ADODB.Recordset, v
v = Range("C6").Value 'Activesheet? Really need a specific worksheet here
If Len(v) > 0 Then
RECSET.Open " select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, " & _
" proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.no_police = " & v & " and sousc.cd_dossier = 'SOUSC' and " & _
" sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit" & _
" and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
With Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient")
If Not RECSET.EOF Then
.Value = RECSET.Fields("b_perf_ctrat_gar").Value
Else
.Value = "0"
End If
End With
RECSET.Close
End If
End Sub
如果 sousc.no_police
不是数字,则在值周围添加单引号。
我想从 C6 中获取我的 sql 查询的值。但是,如果 C6 不为空,我想这样做。我不知道如何在我的代码中翻译这个选项。我的代码是:
Public Sub INFO_PROTO34(ByRef strQ As String)
Dim RECSET As New ADODB.Recordset
RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.no_police = Range("C6") and sousc.cd_dossier = 'SOUSC' and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
If Not RECSET.EOF Then
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = RECSET.Fields("b_perf_ctrat_gar").Value
Else
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = "0"
End If
RECSET.Close
End Sub
非常感谢您的建议
在RECSET.OPEN
之前你可以尝试使用IsEmpty
方法
我还没有在 VBA 脚本上对此进行测试,但这里是我如何更改您的代码以根据其是否为空白来考虑 C6 单元格:
Public Sub INFO_PROTO34(ByRef strQ As String)
Dim RECSET As New ADODB.Recordset
if IsEmpty(Range("C6").value) = true then
' if C6 cell is blank or empty it will do the SQL without the C6 cell value
RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.cd_dossier = 'SOUSC' and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
Else
' in this case the cell C6 is not empty/blank so it will use your existing SQL statement
RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.no_police = Range("C6") and sousc.cd_dossier = 'SOUSC' and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
end if
If Not RECSET.EOF Then
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = RECSET.Fields("b_perf_ctrat_gar").Value
Else
Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient").Value = "0"
End If
RECSET.Close
End Sub
有关 IsEmpty
方法的详细信息,请参阅 https://www.techonthenet.com/excel/formulas/isempty.php。
例如:
Public Sub INFO_PROTO34(ByRef strQ As String)
Dim RECSET As New ADODB.Recordset, v
v = Range("C6").Value 'Activesheet? Really need a specific worksheet here
If Len(v) > 0 Then
RECSET.Open " select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, " & _
" proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc,db_produit prod, db_protocole proto" & _
" where sousc.no_police = " & v & " and sousc.cd_dossier = 'SOUSC' and " & _
" sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit" & _
" and '" & strQ & "' = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
With Worksheets("1 - Feuille de Suivi Commercial").Range("Calcul_Perf_Contrat_et_Orient")
If Not RECSET.EOF Then
.Value = RECSET.Fields("b_perf_ctrat_gar").Value
Else
.Value = "0"
End If
End With
RECSET.Close
End If
End Sub
如果 sousc.no_police
不是数字,则在值周围添加单引号。