[英]Find all formulas and drag them down (FillDown) to a certain row number skipping cells that have values in them?
我正在嘗試使用FillDown使所有公式都下拉到某一行,從而跳過其中具有值的單元格...
如果我們看這個例子:
在C和E列中都有公式,在C6中,我們用15值覆蓋了單元格。
如果我們在第5行中,並且我希望代碼將公式向下拖動5行,則需要在C5中使用公式,並且由於C6具有值,因此它將跳過此值,而是將其放入C7並將其向下拖動。
有誰知道如何實現這一目標?
遍歷該列,如果該單元格為空,則從第一個單元格復制公式?
像這樣?
Sub Sample()
Dim rng As Range
Dim lRow As Long, i As Long
'~~> Change this to the relevant sheet name
With ThisWorkbook.Sheets("Sheet1")
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> C2 Which I am assuming will have a formula
Set rng = .Cells(2, 3)
For i = 3 To lRow
If .Cells(i, 3).Value = "" Then rng.Copy .Cells(i, 3)
Next i
End With
End Sub
注意 :對於您的情況,請根據需要進行修改。
編輯
我強烈建議使用@Captains Non VBA方式,如上所示。 這是代碼,以防萬一有人在尋找VBA選項。
Sub Sample()
Dim rng As Range, blnkRng As Range
Dim lRow As Long, i As Long
'~~> Change this to the relevant sheet name
With ThisWorkbook.Sheets("Sheet1")
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> C2 Which I am assuming will have a formula
Set rng = .Cells(2, 3)
On Error Resume Next
Set blnkRng = .Range("C3:C" & lRow).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not blnkRng Is Nothing Then rng.Copy blnkRng
End With
End Sub
如果您不想走VBA路線,則可以使用內置功能:
“特別提示”框中還有其他一些有用的內容!
要使用代碼來做到這一點(假設您想復制到第10行):
Range(“ C3:C10”)。SpecialCells(xlCellTypeFormulas).FormulaR1C1 = Range(“ C2”)。FormulaR1C1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.