【问题标题】:Looping through column and updating Blank cells in Excel循环遍历列并更新 Excel 中的空白单元格
【发布时间】:2021-07-12 22:08:14
【问题描述】:

我正在尝试更新列中的所有空白单元格以允许我的过滤工作。这是我的代码:

 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 
      var = Cells(i, 4).Value
 End If

 Cells(i, 2).Value = var       'Assign value to column 2

 Next i

 End Sub

下图显示了我的代码如何工作的结果,我知道这是错误的。正如您从 H 列中看到的那样,我想要做的是 Level = 2 然后获取 Trailer ID 的值并使用该值更新 Trailer ID2。正如您所看到的,只要我有一个不同的 Trailer Reg,它就会继续使用新值。

.

我想基本上分配级别 2(G 列)的值并将值复制到以下所有组下的(H 列),即 > 级别 2 已完成。因此,当您达到新的 2 级时,它会重新开始。如果我希望它如何工作,这就是结果:

任何帮助将不胜感激。谢谢你

【问题讨论】:

    标签: excel vba loops filter count


    【解决方案1】:

    始终尝试完全限定您的代码。

    您的代码似乎与您描述的不符,应该这样做:

    每一行

    1. 检查级别(C 列)是否为 2 - 如果为真则存储预告片 ID

    2. 如果存在则插入当前行的Trailer ID,否则将存储的Trailer ID插入H列

      Sub MyLoop()

      Dim i As Long
      Dim LastRow As Long
      Dim trailerID As String
      
      With Sheet1 'Change to the correct worksheet reference
      
          LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
      
          For i = 2 To LastRow 'This should start from row 2 or 3? After the header row
              If .Cells(i, 3).Value = 2 Then 'Check the value in Column C (Level) if it's 2
                  trailerID = .Cells(i, 7).Value
              End If
      
              If .Cells(i, 7).Value = vbNullString Then
                  .Cells(i, 8).Value = trailerID      'If Trailer ID column is empty, take the last stored Trailer ID from Level 2
              Else
                  .Cells(i, 8).Value = .Cells(i, 7).Value 'If not empty, take from the current row's Trailer ID
              End If
          Next i
      End With
      

      结束子

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多