Excel 循环到最后一行

Excel 循环到最后一行,excel,vba,Excel,Vba,我有两列A和B 哪里 A=天 B=取决于A的括号 我已经做了代码,如果不是的话 Sub AA() If Range("A2").Value <= 0 Then Range("B2").Value = "Not Due" ElseIf Range("A2").Value >= 1 And Range("A2").Value

我有两列A和B 哪里 A=天 B=取决于A的括号

我已经做了代码,如果不是的话

        Sub AA()
    If Range("A2").Value <= 0 Then
            Range("B2").Value = "Not Due"
    
    ElseIf Range("A2").Value >= 1 And Range("A2").Value <= 30 Then
        Range("B2").Value = "1-30 Days"
    
    ElseIf Range("A2").Value >= 31 And Range("A2").Value <= 90 Then
        Range("B2").Value = "31-90 Days"
    
    ElseIf Range("A2").Value >= 91 And Range("A2").Value <= 180 Then
        Range("B2").Value = "91-180 Days"
    
    ElseIf Range("A2").Value >= 181 And Range("A2").Value <= 365 Then
        Range("B2").Value = "181-365 Days"
    
    ElseIf Range("A2").Value >= 366 And Range("A2").Value <= 730 Then
        Range("B2").Value = "1-2 Years"
    
    ElseIf Range("A2").Value >= 731 And Range("A2").Value <= 1095 Then
        Range("B2").Value = "2-3 Years"
    
    ElseIf Range("A2").Value >= 1096 Then
        Range("B2").Value = "Over 3 Years"
    End If 
End sub
Sub-AA()
如果范围(“A2”).值=1,范围(“A2”).值=31,范围(“A2”).值=91,范围(“A2”).值=181,范围(“A2”).值=366,范围(“A2”).值=731,范围(“A2”).值=1096,则
范围(“B2”)。值=“超过3年”
如果结束
端接头
以上代码只在一个单元格上运行我需要运行此代码直到最后一行

谢谢

Excel不理解“最后一行”:这是一个电子表格,您可以随意填写。
您确实可以一直到列中最后一个填充的单元格(在VBA中,对于
范围
对象,这称为
.End(xlDown)
),但您确实需要小心:假设您有一个类似这样的Excel工作表:

        Col1   Col2
Row1  Val1.1 Val1.2
Row2  Val2.1 Val2.2

Rowa  Vala.1 Vala.2
Rowb  Valb.1 Valb.2
想象一下,重点是Val1.2。在VBA中执行
.End(xlDown)
操作时,或按Ctrl+Down键时,将转到Val2.2,而不是Valb.2。因此,我建议您对此要非常谨慎。

小括号()
    Sub Bracket()

Dim Cell As Range

  For Each Cell In Range("A2:A1048576")
    If Cell.Value <= 0 Then
      Cell.Offset(0, 1).Value = "Not Due"
    ElseIf Cell.Value >= 1 And Cell.Value <= 30 Then
      Cell.Offset(0, 1).Value = "1-30 Days"
    ElseIf Cell.Value >= 31 And Cell.Value <= 90 Then
      Cell.Offset(0, 1).Value = "31-90 Days"
    ElseIf Cell.Value >= 91 And Cell.Value <= 180 Then
      Cell.Offset(0, 1).Value = "91-180 Days"
    ElseIf Cell.Value >= 181 And Cell.Value <= 365 Then
      Cell.Offset(0, 1).Value = "181-365 Days"
    ElseIf Cell.Value >= 366 And Cell.Value <= 730 Then
      Cell.Offset(0, 1).Value = "1-2 Years"
    ElseIf Cell.Value >= 731 And Cell.Value <= 1095 Then
      Cell.Offset(0, 1).Value = "2-3 Years"
    ElseIf Cell.Value >= 1096 Then
      Cell.Offset(0, 1).Value = "2-3 Years"
         End If
  Next Cell
 
End Sub
暗淡单元格作为范围 对于范围内的每个单元格(“A2:A1048576”) 如果Cell.Value=1,Cell.Value=31,Cell.Value=91,Cell.Value=181,Cell.Value=366,Cell.Value=731,Cell.Value=1096,则 单元偏移量(0,1).Value=“2-3年” 如果结束 下一个细胞 端接头
试试这个:

Sub subLoopRows()

    Dim lngRow As Long
    Dim lngLastUsedRow As Long

    'Finding out the last used row
    lngLastUsedRow = ActiveSheet.UsedRange.Rows.Count
    
    'Loop from the first row till the last used row. Set the start row as 2 if you have a header
    For lngRow = 1 To lngLastUsedRow
        
        With ActiveSheet
            
            If .Cells(lngRow, 1).Value <= 0 Then
            
                .Cells(lngRow, 2).Value = "Not Due"
                
            ElseIf .Cells(lngRow, 1).Value >= 1 _
                And .Cells(lngRow, 1).Value <= 30 Then
                
                .Cells(lngRow, 2).Value = "1-30 Days"
                
            ElseIf .Cells(lngRow, 1).Value >= 91 _
                And .Cells(lngRow, 1).Value <= 180 Then
                
                .Cells(lngRow, 2).Value = "91-180 Days"
                
            ElseIf .Cells(lngRow, 1).Value >= 181 _
                And .Cells(lngRow, 1).Value <= 365 Then
                
                .Cells(lngRow, 2).Value = "181-365 Days"
                
            ElseIf .Cells(lngRow, 1).Value >= 366 _
                And .Cells(lngRow, 1).Value <= 730 Then
                
                .Cells(lngRow, 2).Value = "1-2 Years"
        
            ElseIf .Cells(lngRow, 1).Value >= 731 _
                And .Cells(lngRow, 1).Value <= 1095 Then
                
                .Cells(lngRow, 2).Value = "2-3 Years"
                
            ElseIf .Cells(lngRow, 1).Value >= 1096 Then
                
                .Cells(lngRow, 2).Value = "Over 3 Years"
                
            End If
        
        End With
    
    Next lngRow

End Sub
子循环行()
长得一样长
昏暗的夜空和长长的夜空一样
'查找最后使用的行
lngLastUsedRow=ActiveSheet.UsedRange.Rows.Count
'从第一行循环到最后使用的行。如果有标题,则将起始行设置为2
对于lngRow=1到lngLastUsedRow
使用ActiveSheet
If.Cells(lngRow,1).Value=1_
和.Cells(lngRow,1)。值=91_
和.Cells(lngRow,1).Value=181_
和。单元格(lngRow,1)。值=366_
和.Cells(lngRow,1).Value=731_
和.Cells(lngRow,1).Value=1096然后
.Cells(lngRow,2).Value=“超过3年”
如果结束
以
下一个成长
端接头

希望这是有帮助的

如果您添加一些示例数据,将有助于了解如何查找最后一行。在SE和上,使用您的确切问题,有无数的示例说明如何查找最后一行。你试过什么?为什么使用VBA?这是使用查找表很容易解决的问题。