簡體   English   中英

用基於另一個單元格值的非空白單元格填充空白單元格值

[英]Fill blank cell value with non blank cell based another cell value

我在填充列的空白單元格時遇到問題。

我在 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

字典可能是矯枉過正,但這應該工作。

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

這似乎可行,它基於 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

您可以在使用公式之前對列表進行排序。 像這樣的東西可能會起作用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM