跳过一行而不是“For Each row In rng.Rows”的 VBA 代码

跳过一行而不是“For Each row In rng.Rows”的 VBA 代码

希望有人能帮助我解决我的 VBA 代码。

现在它说:如果 cell.Value = “ja” 并且 (cell.Offset(0, 9) = “meenemen”) 则...

我希望它是这样的:如果 cell.Value =“ja”且(cell.Offset(0, 9)=“meenemen”)或((cell.Offset(1, 0)=“ja”且(cell.Offset(1, 9)=“meenemen”))则...

我现在使用:对于 rng.Rows 中的每一行,但我希望它在继续之前仅检查每隔一行(并且在我的编码的另一部分中每 6 行检查一次)。

不幸的是,我无法更改加载数据的工作表的布局。

有人能给我一个好的解决方案吗?提前感谢你的帮助。

它所涉及的 VBA 代码部分现在如下所示:

子检查范围3()

暗淡 rng 作为范围 暗淡行作为范围 暗淡单元格作为范围

设置 rng = Range("C47:C66")

对于 rng.Rows 中的每一行 对于 row.Cells 中的每一单元格 如果 cell.Value = "ja" 且 (​​cell.Offset(0, 9) = "meenemen") 则

    Dim Name As String
    DateStr = Format(Date, "dd-mm-yy")
    Name = cell.Offset(0, -1).Text & "-" & Range("A44") & " " & DateStr
    cell.Offset(0, 12).Value = Name

    Dim startPath As String
    Dim myName As String

    startPath = "I:\Medische Microbiologie\Virologie\Sequence-resultaten\@In bewerking\"
    myName = cell.Offset(0, 12).Text       ' Change as required to cell holding the folder title

    If myName = vbNullString Then myName = "Testing"

    Dim folderPathWithName As String
    folderPathWithName = startPath & Application.PathSeparator & myName

    If Dir(folderPathWithName, vbDirectory) = vbNullString Then
        MkDir folderPathWithName
    Else
        On Error Resume Next
       'Delete files
        FSO.deletefile MyPath & "\*.*", False
        'Delete subfolders
        FSO.deletefolder MyPath & "\*.*", False
        On Error GoTo 0

    End If

    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("F6").Value = cell.Offset(0, 6).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("A10").Value = cell.Offset(0, -1).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("B10").Value = cell.Offset(0, 7).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("C10").Value = cell.Offset(0, 1).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("E10").Value = cell.Offset(0, 4).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("F10").Value = cell.Offset(0, 5).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("G10").Value = cell.Offset(0, 8).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("E11").Value = cell.Offset(1, 4).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("F11").Value = cell.Offset(1, 5).Text
    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Range("G11").Value = cell.Offset(1, 8).Text

    ThisWorkbook.Sheets("BLAST resultaten LSU-ITS").Copy
    ActiveWorkbook.SaveAs (startPath & myName & "\BLAST resultaten " & Name)
    ActiveWorkbook.Close

Else
    cell.Offset(0, 12).ClearContents
End If

下一个单元格 下一行

'文件夹工作

子目录结束

答案1

您应该使用For Next循环和Step关键字来指定增量。

Dim rowIterator As Long
For rowIterator = 1 To rng.Rows.Count Step 2 ' Will go through every other row from the first to the last (or last-1 depending on parity of your number of rows)
    Set row = rng.Rows(rowIterator)
    ' Your code
Next rowIterator

相关内容