【问题标题】:Loop as if condition像条件一样循环
【发布时间】:2017-03-08 22:09:06
【问题描述】:

我有以下问题。

在下面的代码中,我试图让我的 sub 在 if then 语句之后运行一个循环,然后让代码返回第一个循环并从下一个 i 开始。在我下面的代码中,一切正常,但是当满足第一个 IF- 语句中的条件时,它会启动第二个循环,然后立即退出它而不运行它。

所以我的问题是,如何在 IF- 语句中的 then 语句之后创建循环?

Sub Sort3()

    Dim i As Integer
    Dim LastRow As Long
    Dim lenght As String
    Dim LastRow_2 As String
    Dim L_text As Variant
    Dim R_text As Variant
    Dim M_text As Variant

    ThisWorkbook.Sheets("EQ_CLEAN").Select

    LastRow = Range("G" & Rows.Count).End(xlUp).Row

    LastRow_2 = Range("J" & Rows.Count).End(xlUp).Row

    For i = 2 To LastRow

        lenght = Range("G" & i)

        If Len(lenght) = 25 Then

            L_text = Left(Range("A" & i), 12)
            R_text = Right(Range("A" & i), 12)

            For x = 2 To Last_row_2

                On Error Resume Next
                n = Worksheets("EQ_CLEAN").Range("D1:D6000").Cells.SpecialCells(xlCellTypeConstants).Count

                If L_text <> Sheets("EQ_CLEAN").Range("J" & x) Then

                    Sheets("EQ_CLEAN").Range(Cells(x, 1), Cells(x, 2)).Select
                    Application.CutCopyMode = False
                    Selection.Copy
                    Range("D" & (n + 1)).Select
                    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
                    SkipBlanks:=False, Transpose:=False
                End If
            Next x
        End If
    Next i
End Sub

【问题讨论】:

  • 注释掉On Error Resume Next,看看你是否遇到了运行时错误。
  • .Select 应该尽可能避免使用良好的 VBA 代码编写习惯。我会leave this here 供您阅读。如果您打算继续编写 VBA,从长远来看,它将对您有所帮助。 :)

标签: excel loops if-statement vba


【解决方案1】:

代码审查

在仔细审查您的代码后,我认为您的问题不适用于您的问题。您存在许多错误,我已在下面为您更正:

Option Explicit
Sub Sort3()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("EQ_CLEAN")
    Dim LastRow As Long, LastRow_2 As Long
    Dim lenght As String
    Dim L_text As String, R_text As String, M_text As String
    Dim n As Long, x As Long, i As Long

    LastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row

    LastRow_2 = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row

    For i = 2 To LastRow

        lenght = ws.Range("G" & i).Value

        If Len(lenght) = 25 Then

            L_text = Left(ws.Range("A" & i).Value, 12)
            R_text = Right(ws.Range("A" & i).Value, 12)

            For x = 2 To LastRow_2

                'On Error Resume Next 'You should never use this unless you know exactly which error
                'is popping up and why it can't be avoided. Usually this is for 1004 errors that occur
                'outside of excel... It's better to use proper error handling instead of skipping all errors.
                'You also never tell excel to recognize errors via On Error GoTo 0. I advise you stay away
                'from handling errors with these two statements.

                'don't make it a habit to assign rogue variables values
                n = ws.Range("D1:D6000").Cells.SpecialCells(xlCellTypeConstants).Count

                If L_text <> ws.Range("J" & x).Value Then
                    ws.Range(ws.Cells(x, 1), ws.Cells(x, 2)).Copy
                    ws.Range("D" & (n + 1)).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
                        SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = xlCopy 'this is the proper format to remove the marching ants
                End If
            Next x
        End If
    Next i
End Sub

我试图将 cmets 放在进行更改的地方。

我在一张白纸上运行了您的代码,它没有抛出任何错误。看看它是否达到了您的要求。


参考文献

Error Handling

Avoiding Use of .Select

What Microsoft says about Option Explicit

Why I changed your Row based variables to Long

【讨论】:

    【解决方案2】:

    总是在开头使用Option Explicit。这会向您表明您调暗并设置LastRow_2,然后尝试使用Last_row_2...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-05
      • 2015-09-19
      • 1970-01-01
      • 2019-05-27
      • 2015-03-20
      • 2020-06-27
      • 2011-03-10
      • 2020-03-11
      相关资源
      最近更新 更多