循环直到最后一行

时间:2020-08-11 17:21:29

标签: 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 <= 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

以上代码仅在一个单元格上起作用我需要将此代码运行到最后一行

谢谢

3 个答案:

答案 0 :(得分:0)

Excel无法理解“最后一行”:这是一个电子表格,您可以根据需要填写。
您确实可以一直到列中的最后一个单元格都已填写(在VBA中,此.End(xlDown)对象称为Range),但是您确实需要小心:假设您有像这样的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。因此,我建议您对此非常谨慎。

答案 1 :(得分:0)

    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

答案 2 :(得分:0)

尝试一下:

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

希望这很有帮助!