【问题标题】:Multiple periods under single For Statement单个 For 语句下的多个期间
【发布时间】:2016-04-20 13:34:34
【问题描述】:

我正在考虑为多个行和列运行 For 循环,但是我正在跳过一些列。

所以 ATM 我的代码与此类似,但这不起作用。如何在不包括我不需要的列的同时表达这个范围的列。

For irow = DateStart To DateEnd

For icolumn = 32 To 40
For icolumn = 43 To 58
For icolumn = 60 To 61
For icolumn = 63 To 67

提前致谢

Sub Button16_Click()

Dim wb As Workbook
Dim ws As Worksheet
Dim Drill As String
Dim postRow As Integer
Dim irow As Integer
Dim icolumn As Integer
Dim DateStart As Integer
Dim DateEnd As Integer
Dim SheetDate As Date


 'Start Date and End Date Row from Drill Data entry Sheet
 DateStart = Sheet16.Cells(4, 9).Value
 DateEnd = Sheet16.Cells(5, 9).Value

postRow = 9 ' posting in Uploadsheet
Sheet1.Select


'Drill1 = Range("C16")



For irow = DateStart To DateEnd
For icolumn = 32 To 40
For icolumn = 43 To 58
For icolumn = 60 To 61
For icolumn = 63 To 67




      If Cells(irow, icolumn) > 0.01 Then
    Sheets("UploadSheet").Cells(postRow, 1) = "A"
    Sheets("UploadSheet").Cells(postRow, 2) = Format(Sheet1.Cells(irow, 2), "yyyymmdd") 'Shift Date
    Sheets("UploadSheet").Cells(postRow, 3) = Sheet1.Cells(irow, 4) 'Shift NS/DS
    Sheets("UploadSheet").Cells(postRow, 4) = Sheet1.Cells(irow, 3) 'equipment type
    Sheets("UploadSheet").Cells(postRow, 5) = Sheet1.Cells(4, icolumn) 'code Type
    Sheets("UploadSheet").Cells(postRow, 6) = Sheet1.Cells(irow, icolumn) 'Hours for code type

    postRow = postRow + 1
    Else
      End If
    Next
  Next



Sheets("UploadSheet").Select

End Sub

【问题讨论】:

    标签: vba excel loops for-loop


    【解决方案1】:

    设置一个不连续的范围并使用列索引序数。

    Dim c As Range
    With Range("AF:AN, AQ:BF, BH:BI, BK:BO")
        For Each c In .Columns
            Debug.Print c.Column
        Next c
    End With
    
    ' 32  33  34  35  36  37  38  39  40  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  60  61  63  64  65  66  67 
    

    像这样把它放到你的代码中。

    Dim c As Range, iRow As Long, iColumn As Long
    Dim postRow As Long, DateStart As Long, DateEnd As Long
    
    'stuff for postRow, DateStart & DateEnd here
    
    With Worksheets("Sheet1")
        For iRow = DateStart To DateEnd
            With Range("AF:AN, AQ:BF, BH:BI, BK:BO")
                For Each c In .Columns
                    iColumn = c.Column
                    If Cells(iRow, iColumn) > 0.01 Then
                        Sheets("UploadSheet").Cells(postRow, 1) = "A"
                        Sheets("UploadSheet").Cells(postRow, 2) = Format(.Cells(iRow, 2), "yyyymmdd") 'Shift Date
                        Sheets("UploadSheet").Cells(postRow, 3) = .Cells(iRow, 4) 'Shift NS/DS
                        Sheets("UploadSheet").Cells(postRow, 4) = .Cells(iRow, 3) 'equipment type
                        Sheets("UploadSheet").Cells(postRow, 5) = .Cells(4, iColumn) 'code Type
                        Sheets("UploadSheet").Cells(postRow, 6) = .Cells(iRow, iColumn) 'Hours for code type
                        postRow = postRow + 1
                    End If
                Next c
            End With
        Next iRow
    End With
    

    【讨论】:

    • 对不起,我对 VBA 还是很陌生。这将如何与我的其余代码一起使用
    • 我留下了广泛通用的代码,因为我不确定在找到这些列后你想对它们做什么。也许您可以编辑您的问题以包含在每列上要执行的操作。
    • 我已将其余代码放入其中。它基本上扫描工作表并收集输出,但是它需要跳过不需要的选定列,这将返回错误值
    • 我不确定是否会有一些冗余,但我将非连续循环放在我认为最合适的位置。
    • 嗯,运气不好没有得到任何输出。不过感谢您的帮助!
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多