【问题标题】:Fill blank cell value with non blank cell based another cell value用基于另一个单元格值的非空白单元格填充空白单元格值
【发布时间】:2021-02-27 08:02:46
【问题描述】:

我在填充列的空白单元格时遇到问题。

我在 A、B、C、D 中有 4 个列标题。

我正在尝试根据附加的数据创建宏来填充动态数据的空白单元格,其中 D 列中的单元格值是随机填充和空白的。空白单元格值需要根据 A 列中提到的值填充..

我已经创建了宏,但它只能用上述值填充空白,并没有得到确切的结果..

有人可以帮忙吗...

以下结果是预期的编码...

下面是我创建的宏

Sub FillblankCells()

lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row

With Range("D2:D" & lr)
.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With


End Sub

【问题讨论】:

  • 您需要使用基于 A 列的查找公式。目前您只是使用上面单元格中的值。
  • @SJR 你能帮帮我吗.. 我正在学习宏..
  • 请注意,由于您的数据没有排序,因此查找不起作用,只有在填写每个值的第一个条目时才会起作用。

标签: excel vba


【解决方案1】:

字典可能有点矫枉过正,但这应该可以。

Sub x()

Dim lr As Long, r As Range
Dim oDic As Object

lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
Set oDic = CreateObject("Scripting.Dictionary")

'store column A for each entry in D
For Each r In Range("D2:D" & lr).SpecialCells(xlCellTypeConstants)
    oDic(r.Offset(, -3).Value) = r.Value
Next r

'retrieve each column A for blanks in D
For Each r In Range("D2:D" & lr).SpecialCells(xlCellTypeBlanks)
    r.Value = oDic(r.Offset(, -3).Value)
Next r

End Sub

【讨论】:

    【解决方案2】:

    这似乎可行,它基于 C 列中的值。

    Sub FillblankCells()
    
        lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
    
        With Range("D2:D" & lr)
            .SpecialCells(xlBlanks).FormulaR1C1 = "=IF(R[-1]C[-1]<RC[-1], R[-1]C,R[1]C)"
            .Value = .Value
        End With
    
    End Sub
    

    【讨论】:

    • @ norie ...如果空白单元格正好在填充单元格的下方或上方,这可以正常工作,但如果它是另一行,则它不起作用..我已经做出了适当的更改...
    【解决方案3】:

    您可以在使用公式之前对列表进行排序。这样的事情可能会奏效:

    Sub FillblankCells()
        
        'Declarations.
        Dim RngList As Range
        Dim DblColumnQuote As Double
        Dim DblColumnBuyerName As Double
        
        'Setting.
        Set RngList = Range("A1:D1")
        DblColumnQuote = 1
        DblColumnBuyerName = 4
        
        'Resetting RngList.
        Set RngList = Range(RngList, RngList.End(xlDown))
        
        'Sorting RngList.
        With RngList.Parent.Sort
            .SortFields.Clear
            .SortFields.Add Key:=RngList.Columns(DblColumnQuote), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlDescending, _
                            DataOption:=xlSortNormal
            .SortFields.Add Key:=RngList.Columns(DblColumnBuyerName), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlAscending, _
                            DataOption:=xlSortNormal
            
            .SetRange RngList
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
            .SortFields.Clear
        End With
        
        'Filling the blank cells of the Buyer Name column in RngList.
        With RngList.Columns(DblColumnBuyerName)
            .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
            .Value = .Value
        End With
        
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2019-10-01
      • 1970-01-01
      • 2013-08-25
      • 2021-06-09
      • 1970-01-01
      相关资源
      最近更新 更多