簡體   English   中英

在If語句-VBA中增加For循環

[英]Increment a For loop inside an If statement -VBA

我需要使用循環刪除電子表格中的列,而不是手動對其中的列進行硬編碼。但是,我得到的只是一個非常無用的“下一步,沒有For錯誤”。

Sub test()

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Dim colNum2 As Integer
colNum2 = 1

For x = 1 To 32

    If Range("A1").Value = "Order No." Then
        Next colNum
    ElseIf Range("B1").Value = "Line No." Then
        Next colNum
    ElseIf Range("C1").Value = "Order Qty." Then
        Next x
    ElseIf Range("D1").Value = "PO" Then
        Next x
    ElseIf Range("E1").Value = "Sched Date" Then
        Next x
    ElseIf Range("F1").Value = "Sched MFG Line" Then
        Next x
    ElseIf Range("G1").Value = "Item No." Then
        Next x
    ElseIf Range("H1").Value = "Item Width" Then
        Next x
    ElseIf Range("I1").Value = "Item Height" Then
        Next x
    ElseIf Range("J1").Value = "SL Color" Then
        Next x
    ElseIf Range("K1").Value = "Frame Option" Then
        Next x
    End If
        'Checks if the cell matches a specific string required by the sorter
        'if TRUE should skip through to the next increment of colNum

    Columns(colNum2).EntireColumn.Delete
        'uses the current number of colNum to delete the current column number
    colNum2 = colNum2 + 1

Next x
    'increments colNum by one
    'Iterates next through the loop

我覺得這可以和Java或Python一起使用,所以我很惱火,VBA不允許我這樣做。

有人可以解釋這段代碼出了什么問題嗎?

只需使用var = var + 1代替Next Next結束For循環。 同樣,您也不需要在下一行中重復變量名稱,因為它已經在For行中。 For i = 0 To 5 ... Next

For x = 1 To 32

    If Range("A1").Value = "Order No." Then
        colNum = colNum +1
    ElseIf Range("C1").Value = "Order Qty." Then
        x = x + 1
    End If
Next

請記住Scott Cranner所說的, Next也將執行x=x+1 ,因此,如果您只想每個周期增加一次,請使用Do While周期

x = 1
Do While x <= 32

    If Range("A1").Value = "Order No." Then
        colNum = colNum +1
    ElseIf Range("C1").Value = "Order Qty." Then
        x = x + 1
    End If
Loop

在我看來,您想刪除所有與“排序器所需的特定字符串”不匹配的列。 在這種情況下,您可以遍歷所有的列標題標簽,刪除不匹配的標題標簽,或者使用自定義的從左到右排序將所有不匹配的列放在右邊,然后刪除然后成批

方法1-刪除不匹配的列

Sub test1()
    Dim c As Long, vCOLs As Variant

    vCOLs = Array("Order No.", "Line No.", "Order Qty.", "PO", _
                  "Sched Date", "Sched MFG Line", "Item No.", _
                  "Item Width", "Item Height", "SL Color", "Frame Option")

    With Application
        '.ScreenUpdating = False
        '.EnableEvents = False
    End With

    With Worksheets("sheet1")
        With .Cells(1, 1).CurrentRegion
            'delete from right-to-left or risk missing one
            For c = .Columns.Count To 1 Step -1
                If IsError(Application.Match(.Cells(1, c).Value2, vCOLs, 0)) Then
                    .Columns(c).EntireColumn.Delete
                End If
            Next c
        End With
    End With

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

方法2-自定義排序,然后偏移並刪除

Sub test2()
    Dim vCOLs As Variant

    vCOLs = Array("Order No.", "Line No.", "Order Qty.", "PO", _
                  "Sched Date", "Sched MFG Line", "Item No.", _
                  "Item Width", "Item Height", "SL Color", "Frame Option")

    With Application
        '.ScreenUpdating = False
        '.EnableEvents = False
        .AddCustomList ListArray:=vCOLs
    End With

    With Worksheets("sheet1")
        With .Cells(1, 1).CurrentRegion
            'custom sort to bring the important fields to the left
            .Cells.Sort Key1:=.Rows(1), Order1:=xlAscending, _
                        Orientation:=xlLeftToRight, Header:=xlNo, _
                        OrderCustom:=Application.GetCustomListNum(vCOLs)

            'offset and delete the unwanted columns
            With .Offset(0, Application.Match(vCOLs(UBound(vCOLs)), .Rows(1), 0))
                .EntireColumn.Delete
            End With
        End With
    End With

    With Application
        .DeleteCustomList .GetCustomListNum(vCOLs)
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

無論使用哪種方法,您都只需列出要保留的列並刪除其余的列。


.Cells.Sort.SortFields.Add.Cells.Sort之間通常會有一些混淆。 .SortFields.Add方法使用CustomOrder:=參數,而Range.Sort方法使用OrderCustom:=參數。 兩者絕對是不一樣的,但經常互換使用,結果不佳。

我懷疑您正在嘗試根據第1行中的文本值刪除列。這將為您提供所需的內容,只需將要刪除的所有文本引用放在CASE語句中即可。

Option Explicit

Sub DeleteColumns()

    Dim colNum As Integer

    colNum = 1

    Do While Range(alphaCon(colNum) & 1).Value <> ""

        Select Case Range(alphaCon(colNum) & 1).Value

            Case "ColumnIDontWant", "AnotherColumnIDontWant"

            Columns(colNum).EntireColumn.Delete

        End Select


        colNum = colNum + 1

    Loop

End Sub

Public Function alphaCon(aNumber As Integer) As String
' Fixed version 27/10/2011

Dim letterArray As String
Dim iterations As Integer

letterArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    If aNumber <= 26 Then

        alphaCon = (Mid$(letterArray, aNumber, 1))

    Else

        If aNumber Mod 26 = 0 Then

            iterations = Int(aNumber / 26)
            alphaCon = (Mid$(letterArray, iterations - 1, 1)) & (Mid$(letterArray, 26, 1))

        Else

            'we deliberately round down using 'Int' as anything with decimal places is not a full iteration.
            iterations = Int(aNumber / 26)
            alphaCon = (Mid$(letterArray, iterations, 1)) & (Mid$(letterArray, (aNumber - (26 * iterations)), 1))

        End If

    End If

End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM