查找循环内下一个空行的索引(VB Excel)

lsmd5eda  于 2023-02-10  发布在  其他
关注(0)|答案(4)|浏览(127)

我有一个Excel工作表充满了数据节,每个数据节由一个空行分隔。
当我在工作表的每一行上循环时,我需要找到下一个空白行的索引,这样我就可以知道当前数据节的结束位置,并在传递到下一个数据节之前对其进行修改。
下面是我的第一个循环的例子(在这个循环中,我需要找到下一个空行的索引):

Dim x As Integer
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row    

Range("A1").Select
For x = 1 To lastrow
    If Left(Cells(x, "A").Value, 8) = "!JOURNAL" And Not (IsEmpty(Cells(x, "H"))) Then
          
'''Here I need to add another loop to find the index of my next blank row please'''
            idxblankrow = Range(Cells(x, "A")).CurrentRegion.Row
            MsgBox "Idx blank row is " & idxblkrow
            Range(Cells(x + 2, "A"), Cells(idxblankrow - 1, "H")).Cut Range(Cells(x + 2, "B"), Cells(idxblankrow - 1, "I"))
            Range(Cells(x, "H")).Select
            Selection.Copy
            Range(Cells(x + 2, "A"), Cells(idxblankrow - 1, "A")).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Application.CutCopyMode = False
        End If
    Next

下面是另一个失败的尝试(第二个嵌套的For循环试图搜索空行):

Dim x As Integer
Dim lastrow As Long

lastrow = Cells(Rows.Count, "A").End(xlUp).Row
 
   
For x = 1 To lastrow
    If Left(Cells(x, "A").Value, 8) = "!JOURNAL" And Not (IsEmpty(Cells(x, "H"))) Then
        For j = x To lastrow
            If IsEmpty(Cells(j, "A")) Then idxblankrow = Cells(j, "A").Row
            MsgBox "blank row " & idxblankrow
                Exit For
            End If
                        
        
        Range(Cells(x + 2, "A"), Cells(idxblankrow - 1, "H")).Cut Range(Cells(x + 2, "B"), Cells(idxblankrow - 1, "I"))
        Range(Cells(x, "H")).Select
        Selection.Copy
        Range(Cells(x + 2, "A"), Cells(idxblankrow - 1, "A")).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    End If
Next

任何形式的帮助都将不胜感激,谢谢!

kx5bkwkv

kx5bkwkv1#

请使用下一种适应方式。它不选择,不使用剪贴板:

For x = 1 To LastRow
    If left(cells(x, "A").Value, 8) = "!JOURNAL" And Not (IsEmpty(cells(x, "H"))) Then
          
            idxblankrow = cells(x, "A").End(xlDown).Row
            MsgBox "Idx blank row is " & idxblankrow 
            Range(cells(x + 2, "A"), cells(idxblankrow - 1, "H")).Cut cells(x + 2, "B")
            'copy the value from "H" on the whole A:A column portion:
            Range("A" & x & ":A" & idxblankrow - 1).Value = cells(x, "H").Value 'not using clipboard...
            
            Stop 'check when stopped here if it did what you need
                 'if so, please press F5 to continue and check again.
            
            'you probably need to increment x to continue iteration after the processed portion
            'something as:
            x = x + (idxblankrow - x) + 2 '???
        End If
Next x

你现在可能需要用已经处理的行数来增加x,但是必须用语言解释你试图完成什么。猜测不是一种合适的工作方式......

wooyq4lh

wooyq4lh2#

如果我想知道一整行是否为空,我只需要连接整行并检查长度,如果长度为零,那么该行为空,否则,该行就不是空的。
参见下面的示例性屏幕截图(只有第四行是空的,这在第四个公式中可见,结果为零):

l0oc07j2

l0oc07j23#

使用标志来标识组的开始和结束。这处理组之间的多个空行。

Sub macro()

   Dim ws As Worksheet
   Dim lastrow As Long, i As Long, n As Long
   Dim x As Long, z As Long
   Dim bStart As Boolean, bEnd As Boolean
   
   Set ws = ThisWorkbook.Sheets("Sheet1")
   n = 0
   With ws
       lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
       For i = 1 To lastrow
       
            ' start of group
            If Len(.Cells(i, "A")) > 0 Then
                bStart = True
                n = n + 1
            End If
            
            ' end of group look ahead
            If Len(.Cells(i + 1, "A")) = 0 Then
                bEnd = bStart
            End If
            
            ' valid range
            If bStart And bEnd Then
                x = i - n + 1 ' first row of group
                MsgBox "Processing rows " & x & " to " & i
                
                If Left(.Cells(x, "A").Value, 8) = "!JOURNAL" _
                    And Not (IsEmpty(Cells(x, "H"))) Then
                        ' process rows x to i
                End If
                ' reset flags
                n = 0
                bStart = False
                bEnd = False
            End If
                    
        Next
    End With
End Sub
jum4pzuy

jum4pzuy4#

所有这些答案都可以简单得多,想想这个:

iNextBlankRow = Sheet1.Range("A" & iNextBlankRow & ":A50").SpecialCells(xlCellTypeBlanks).Cells(1, 1).Row

要进行演示,请运行以下宏:

Sub BlankRowTest()
    Dim iNextBlankRow As Long
    Dim r As Long
    
    iNextBlankRow = 1
    For r = 1 To 50
        If iNextBlankRow <= r Then iNextBlankRow = Sheet1.Range("A" & iNextBlankRow + 1 & ":A50").SpecialCells(xlCellTypeBlanks).Cells(1, 1).Row
        Debug.Print r, iNextBlankRow, "'" & Sheet1.Cells(r, 1).Value & "'"
    Next
End Sub

这段代码循环遍历前50行,寻找下一个空行,找到后赋给变量iNextBlankRow,直到当前行(r)大于或等于INextBlankRow时才更新变量iNextBlankRow,此时我们再从下一行开始查找。

相关问题