【问题标题】:Loop until last row and update cell values when row changes循环直到最后一行并在行更改时更新单元格值
【发布时间】:2021-04-22 03:37:14
【问题描述】:

您好,我正在尝试更新所有行的单元格值,直到行号更改。这是我的代码:

 Sub MyLoop()

 Dim i As Integer
 Dim var As String
 Dim LastRow As Long

 LastRow = Range("A" & Rows.Count).End(xlUp).Row

 i = 1

 var = Cells(i, 4).Value

 For i = 1 To LastRow

    If Range("A" & i).Value = "1" Then

       Cells(i, 2).Value = var
  
    End If

    var = Cells(i, 4).Value

 Next i

 End Sub

我附上了运行例程前后的图像。基本上循环遍历所有行,在 A 列中,数字更改将值存储在 D 列中并将其粘贴到 B 列中,直到行号发生更改。

之前:

之后:

亲切的问候

【问题讨论】:

    标签: excel vba loops for-loop while-loop


    【解决方案1】:

    填充列

    快速修复

    Sub MyLoop()
    
        Dim LastRow As Long
        Dim i As Long
        Dim A As Variant
        Dim D As Variant
        
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        
        For i = 1 To LastRow
            If Cells(i, 1).Value <> A Then
                A = Cells(i, 1).Value
                D = Cells(i, 4).Value
            End If
            Cells(i, 2).Value = D
        Next i
    
    End Sub
    

    更灵活的解决方案

    • 调整常量部分中的值。

    Option Explicit
    
    Sub fillColumn()
        
        ' Define constants.
        Const wsName As String = "Sheet1"
        Const ColumnsAddress As String = "A:D"
        Const LookupCol As Long = 1
        Const CriteriaCol As Long = 4
        Const ResultCol As Long = 2
        Const FirstRow As Long = 2
        
        ' Define Source Range.
        Dim rng As Range
        With ThisWorkbook.Worksheets(wsName).Columns(ColumnsAddress)
            Set rng = .Columns(LookupCol).Resize(.Rows.Count - FirstRow + 1) _
                .Offset(FirstRow - 1).Find( _
                    What:="*", _
                    LookIn:=xlFormulas, _
                    SearchDirection:=xlPrevious)
            If rng Is Nothing Then
                Exit Sub
            End If
            Set rng = .Resize(rng.Row - FirstRow + 1).Offset(FirstRow - 1)
        End With
        
        ' Write values from Source Range to Data Array.
        Dim Data As Variant: Data = rng.Value
        
        ' Define Result Array.
        Dim Result As Variant: ReDim Result(1 To UBound(Data, 1), 1 To 1)
        
        ' Declare additional variables.
        Dim cLookup As Variant ' Current Lookup Value
        Dim cCriteria As Variant ' Current Criteria Value
        Dim i As Long ' Rows Counter
        
        ' Write values from Data Array to Result Array.
        For i = 1 To UBound(Data, 1)
            If Data(i, LookupCol) <> cLookup Then
                cLookup = Data(i, LookupCol)
                cCriteria = Data(i, CriteriaCol)
            End If
            Result(i, 1) = cCriteria
        Next i
        
        ' Write from Result Array to Destination Column Range.
        rng.Columns(ResultCol).Value = Result
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      真的是数字变了还是D列的单词变了?

      Columns("D:D").Cut Destination:=Columns("B:B")
      Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
      Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).Value = Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).Value
      

      【讨论】:

        【解决方案3】:
        Sub MyLoop()
        
         Dim i As Integer
         Dim var As String
         Dim LastRow As Long
        
         LastRow = Range("A" & Rows.Count).End(xlUp).Row
        
         For i = 1 To LastRow
            IF Cells(i, 4).Value<>"" Then 'Get new value from column 4
               var = Cells(i, 4).Value
            End If
        
            Cells(i, 2).Value = var       'Assign value to column 2
        
         Next i
        
         End Sub
        

        【讨论】:

        • 谢谢你。我确实错过了一些非常重要的东西,但我很抱歉 D 列中的值不会是空白的。它们都将具有不同零件名称的值 - 对不起,伙计!所以我需要的是在 D 列中取第一个值并用相同的值填充 B 列。当 A 列发生变化时,它会再次采用 D 列中的值。
        猜你喜欢
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 2018-08-08
        • 2022-06-10
        • 2013-12-12
        • 2019-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多