vba 如果没有...否则跳过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16346985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:56:54  来源:igfitidea点击:

If not...Else skip

vbaif-statementforeachskip

提问by Sean Connecticut

I found a question really close to mine but as the answer was specific to the question I couldn't quite relate it to mine.

我发现了一个非常接近我的问题,但由于答案是针对该问题的,我无法将其与我的问题完全联系起来。

I am using an if not function to populate a column that already has values in it and I want it to overwrite the values if the conditions exist and if they don't I want it to leave the value how it is.

我正在使用 if not 函数来填充已经包含值的列,如果条件存在,我希望它覆盖这些值,如果条件不存在,我希望它保留值原样。

In other words I want the "else" part of the function to say skip or something like that... I've tried "null" "skip" and a few others but no luck.

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

Below is a portion of my code in case it's necessary ignore problems in my code this isn't the finished product just the basic idea in case you needed some variables. Thank you in advance!:

下面是我的代码的一部分,以防万一有必要忽略我的代码中的问题,这不是成品,只是基本思想,以防您需要一些变量。先感谢您!:

Sub ReportCompareAlta() ' ' ReportCompareAlta Macro ' Adds instances where column D is "ALTA"

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

结束子

回答by nullptr

Simply remove the Else:

只需删除 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     

回答by ray

As stated, you can just omit the Else portion.

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

If you want to actually see a "skip", you can do this:

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

Else
   Debug.Print "SKIP"
End If

This will show "Skip" in the Immediate Window (Ctrl + G)

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