Excel 检查for循环中每个迭代的最后一行

Excel 检查for循环中每个迭代的最后一行,excel,vba,Excel,Vba,这是我的代码: For Each cell In Range("D2:D" & LastCompoundInterval) LastTransaction = Range("H" & Rows.Count).End(xlUp).Row If <some condition> ... If <some condition> ...

这是我的代码:

 For Each cell In Range("D2:D" & LastCompoundInterval)
        LastTransaction = Range("H" & Rows.Count).End(xlUp).Row
        If <some condition>
        ...
        If <some condition>
        ...
        End If
        Range("H" & LastTransaction) = DateAdd("m", m, cell.Offset(0, -2).Value)
        Range("I" & LastTransaction) = 0
        End If
Next cell
范围内每个单元格的
(“D2:D”和lastcompondinterval)
LastTransaction=范围(“H”和Rows.Count).End(xlUp).Row
如果
...
如果
...
如果结束
范围(“H”&LastTransaction)=日期添加(“m”,m,单元格偏移量(0,-2).Value)
范围(“I”&LastTransaction)=0
如果结束
下一个细胞

我在
H
I
列的末尾追加值,这两列的行数相同,但最后一行的
LastTransaction
变量在每次迭代中保持不变。它不应该更新每个迭代吗?

LastTransaction
来自
范围(“H”和Rows.Count)。End(xlUp)。Row
。它将获取H列中最后一个非空单元格的行号

Rows.Count
这里指的是活动工作表中的总行数,它是一个常量。整个表达式与以下手动操作相同:

  • 找到H列中的最后一个单元格(行数最大)
  • 点击ctrl+向上箭头,向上搜索下一个非空白单元格
  • 返回该单元格的行号

  • LastTransaction
    来自
    范围(“H”和Rows.Count).End(xlUp).Row
    。它将获取H列中最后一个非空单元格的行号

    Rows.Count
    这里指的是活动工作表中的总行数,它是一个常量。整个表达式与以下手动操作相同:

    • 找到H列中的最后一个单元格(行数最大)
    • 点击ctrl+向上箭头,向上搜索下一个非空白单元格
    • 返回该单元格的行号

    首先,您的问题的答案不应该每次迭代都更新吗?是的。它更新了LastTransaction,但保持不变

    如果要转到下一行,需要在下一行之前重新计算上一行

     LastTransaction = Range("H" & Rows.Count).End(xlUp).Row  'Maybe you also need to add a +1 here if you want to have the first empty cell in column H
     For Each cell In Range("D2:D" & LastCompoundInterval)
            ' LastTransaction = Range("H" & Rows.Count).End(xlUp).Row
            If <some condition>
            ...
            If <some condition>
            ...
            End If
            Range("H" & LastTransaction) = DateAdd("m", m, cell.Offset(0, -2).Value)
            Range("I" & LastTransaction) = 0
            End If
            LastTransaction = Range("H" & Rows.Count).End(xlUp).Row +1
    Next cell
    
    LastTransaction=Range(“H”&Rows.Count).End(xlUp).Row“如果希望在H列中有第一个空单元格,可能还需要在此处添加+1
    对于范围内的每个单元格(“D2:D”和LastCompoundInterval)
    'LastTransaction=Range(“H”和Rows.Count).End(xlUp).Row
    如果
    ...
    如果
    ...
    如果结束
    范围(“H”&LastTransaction)=日期添加(“m”,m,单元格偏移量(0,-2).Value)
    范围(“I”&LastTransaction)=0
    如果结束
    LastTransaction=范围(“H”和Rows.Count)。结束(xlUp)。行+1
    下一个细胞
    
    首先,您的问题的答案不应该每次迭代都更新吗?是的。它更新了LastTransaction,但保持不变

    如果要转到下一行,需要在下一行之前重新计算上一行

     LastTransaction = Range("H" & Rows.Count).End(xlUp).Row  'Maybe you also need to add a +1 here if you want to have the first empty cell in column H
     For Each cell In Range("D2:D" & LastCompoundInterval)
            ' LastTransaction = Range("H" & Rows.Count).End(xlUp).Row
            If <some condition>
            ...
            If <some condition>
            ...
            End If
            Range("H" & LastTransaction) = DateAdd("m", m, cell.Offset(0, -2).Value)
            Range("I" & LastTransaction) = 0
            End If
            LastTransaction = Range("H" & Rows.Count).End(xlUp).Row +1
    Next cell
    
    LastTransaction=Range(“H”&Rows.Count).End(xlUp).Row“如果希望在H列中有第一个空单元格,可能还需要在此处添加+1
    对于范围内的每个单元格(“D2:D”和LastCompoundInterval)
    'LastTransaction=Range(“H”和Rows.Count).End(xlUp).Row
    如果
    ...
    如果
    ...
    如果结束
    范围(“H”&LastTransaction)=日期添加(“m”,m,单元格偏移量(0,-2).Value)
    范围(“I”&LastTransaction)=0
    如果结束
    LastTransaction=范围(“H”和Rows.Count)。结束(xlUp)。行+1
    下一个细胞
    
    我认为您的代码应该是:

     For Each cell In Range("D2:D" & LastCompoundInterval)
            LastTransaction = Range("H" & Rows.Count).End(xlUp).Row
            If <some condition>
            ...
            If <some condition>
            ...
            End If
            Range("H" & LastTransaction + 1) = DateAdd("m", m, cell.Offset(0, -2).Value)
            Range("I" & LastTransaction + 1) = 0
            End If
    Next cell
    
    范围内每个单元格的
    (“D2:D”和lastcompondinterval)
    LastTransaction=范围(“H”和Rows.Count).End(xlUp).Row
    如果
    ...
    如果
    ...
    如果结束
    范围(“H”和LastTransaction+1)=日期添加(“m”,m,单元格偏移量(0,-2).Value)
    范围(“I”&LastTransaction+1)=0
    如果结束
    下一个细胞
    

    以前,它只重写列中最后一行的值,而没有将另一个值添加为另一行<代码>+1将新条目放在下一行。

    我认为您的代码应该是:

     For Each cell In Range("D2:D" & LastCompoundInterval)
            LastTransaction = Range("H" & Rows.Count).End(xlUp).Row
            If <some condition>
            ...
            If <some condition>
            ...
            End If
            Range("H" & LastTransaction + 1) = DateAdd("m", m, cell.Offset(0, -2).Value)
            Range("I" & LastTransaction + 1) = 0
            End If
    Next cell
    
    范围内每个单元格的
    (“D2:D”和lastcompondinterval)
    LastTransaction=范围(“H”和Rows.Count).End(xlUp).Row
    如果
    ...
    如果
    ...
    如果结束
    范围(“H”和LastTransaction+1)=日期添加(“m”,m,单元格偏移量(0,-2).Value)
    范围(“I”&LastTransaction+1)=0
    如果结束
    下一个细胞
    

    以前,它只重写列中最后一行的值,而没有将另一个值添加为另一行<代码>+1在下一行中创建新条目。

    这只回答“为什么变量保持不变”。当我还在试图弄清楚“每个迭代中的最后一行”和“追加”的定义是什么意思时,我看到了@JMP的答案。你可以参考它。这只回答“为什么变量保持不变”。当我还在试图弄清楚“每个迭代中的最后一行”和“追加”的定义是什么意思时,我看到了@JMP的答案。你可以参考一下。