如果不是......否则跳过

时间:2013-05-02 20:18:33

标签: vba if-statement foreach skip

我发现了一个非常接近我的问题,但由于答案是针对这个问题的,所以我无法将它与我的问题联系起来。

我正在使用if not函数来填充已经包含值的列,如果条件存在,我希望它覆盖值,如果不存在,我希望它保留值的方式。< / p>

换句话说,我希望函数的“else”部分说出跳过或类似的东西......我尝试过“null”“skip”和其他一些但没有运气。

以下是我的代码的一部分,如果有必要忽略我的代码中的问题,这不是完成的产品,只是你需要一些变量的基本想法。提前谢谢!:

Sub ReportCompareAlta() “ 'ReportCompareAlta Macro '添加列D为“ALTA”的实例

Dim varSheetA As Variant
Dim varSheetB As Variant
Dim varSheetC As Variant
Dim StrValue As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim WbkA As Workbook
Dim WbkB As Workbook
Dim WbkC As Workbook
Dim counter As Long

Set WbkA = Workbooks.Open(Filename:="G:\Reporting\AH_MISSE_FEB2013.xls")
Set WbkB = Workbooks.Open(Filename:="G:\Reporting\AH_MISSE_MAR2013.xls")
Set WbkC = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls")
Set varSheetA = WbkA.Worksheets("LocalesMallContratos")
Set varSheetB = WbkB.Worksheets("LocalesMallContratos")
Set varSheetC = WbkC.Worksheets("Sheet1")


strRangeToCheck = "A1:IV65536"

Debug.Print Now
varSheetA = WbkC.Worksheets("Sheet2").Range(strRangeToCheck) 'may be confusing code here
varSheetB = WbkC.Worksheets("Sheet3").Range(strRangeToCheck) 'may be confusing code here
Debug.Print Now

counter = 0

For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)        
        If varSheetB(iRow, "B") = varSheetA(iRow, "B") & varSheetB(iRow, "B") <> "GERENCIA" & varSheetB(iRow, "B").Value <> "" & varSheetB(iRow, "D") = "ALTA" Then
            StrValue = ""
            varSheetB.Range("iRow:B").Select 
            Selection = StrValue
            ActiveSheet = varSheetC
            Range("A1").Select
            Selection.Offset(counter, 0).Value = StrValue
            counter = counter - 1

        Else
            "SKIP"
        End If         
Next iRow

End Sub

2 个答案:

答案 0 :(得分:2)

只需删除Else:

    If varSheetB(iRow, "B") = varSheetA(iRow, "B") & varSheetB(iRow, "B") <> "GERENCIA" & varSheetB(iRow, "B").Value <> "" & varSheetB(iRow, "D") = "ALTA" Then
        StrValue = ""
        varSheetB.Range("iRow:B").Select 
        Selection = StrValue
        ActiveSheet = varSheetC
        Range("A1").Select
        Selection.Offset(counter, 0).Value = StrValue
        counter = counter - 1

    End If     

答案 1 :(得分:1)

如上所述,您可以省略Else部分。

如果你想真正看到“跳过”,你可以这样做:

Else
   Debug.Print "SKIP"
End If

这将在立即窗口中显示“跳过”( Ctrl + G