多个If语句VBA

时间:2016-01-28 11:41:25

标签: vba if-statement

我对VBA和SQL非常陌生,我目前正在构建一个表,将其上传到Excel并使用VBA。 我想基本上说,如果列I(检查市场)或J(检查m2)的值是否为#NA,则不再进行,不执行上传或其余代码。我认为其中一个问题可能是我已经有一个IF循环 - 这是成功的并且没有与之相关的错误。

这是我目前的代码

'Where Marked sht.Cells(Row,15) = "x" 'FIRST IF LOOP
    If sht.Cells(lRow, 15) = "X" Then

  'If I or J columns say #N/A then DO NOT continue
If IsError(sht.Cells(lRow, 9).Value) Then MsgBox "Error in Column 'Check Market'"
If IsError(sht.Cells(lRow, 10).Value) Then MsgBox "Error in Column 'Check m2'"
''''At the moment it is the above part that isn't successfully running, it notifies the user of an error but doesn't stop the process.

  'Change blank spaces to be Null
 *******






    sSQL = *******Main part of code goes here******                        




     'execute queries
        ********



  'Put back all the 'null' values to blank
      '''''     




End If 'END OF IF X LOOP

2 个答案:

答案 0 :(得分:0)

我不清楚是否有可能两列都有错误,但为了以防万一,我已提供此列。

'If I or J say #N/A then dont proceed with upload.
 If iserror(sht.Cells(lRow, 9).value) and iserror(sht.Cells(lRow, 10).value) Then
     MsgBox "Errors in both Columns"

 ElseIf iserror(sht.Cells(lRow, 9).value) Then
     MsgBox "Error in Column 'Check Market'"

 ElseIf iserror(sht.Cells(lRow, 10).value) Then
     MsgBox "Error in Column 'Check KPI'"

 Else: 'Continue

 End if

答案 1 :(得分:0)

只需添加原始代码

I(9):Check Market J(10):Check m2 
'If I or J say #N/A then dont proceed with upload.
    If sht.Cells(lRow, 9).Value = "#N/A" Then

        MsgBox "Error in Column 'Check Market'"
        Exit Sub

    ElseIf sht.Cells(lRow, 10).Value = "#N/A" Then

        MsgBox "Error in Column 'Check KPI'"
        Exit Sub

    Else: 'Continue

退出的代码MsgBox

Dim response
response = MsgBox("My message here.", vbYesNo, "My Title")
If response = vbNo Then
    Exit Sub
End If
MsgBox ("You clicked YES.")