[英]Loop paste formula until next cell in range is empty
I am trying to paste a formula next to range of cells, but only the one's that contains a value, the script must loop until the next cell in the range is empty. 我试图在一系列单元格旁边粘贴一个公式,但只有一个包含一个值的公式,脚本必须循环,直到该范围中的下一个单元格为空。 For instance Sheet 1 Column A
contains date until row 12, then I would like to paste a formula in column D2:D12
Regards 例如,工作表1列A
包含直到第12行的日期,然后我想在D2:D12
列中粘贴公式D2:D12
问候
Like this? 像这样?
Option Explicit
Sub Sample()
Dim lastRow As Long, i As Long
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
With ws
For i = 1 To lastRow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then _
.Range("D" & i).Formula = "YOUR FORMULA"
Next i
End With
End Sub
As you are looking down to the first blank cell then you can avoid a loop and use 当您俯视第一个空白单元格时,您可以避免循环并使用
The code includes a test to make sure that the code doesn't proceed if all of column A is blank - ie if the range from A1 down extends to the bottom of the sheet and A1 is blank 该代码包括一个测试,以确保如果所有A列都为空,则代码不会继续 - 即,如果A1向下的范围延伸到工作表的底部,并且A1为空
This code adds a sample formula linking each cell in column D to the respective row in column B 此代码添加了一个样本公式,将D列中的每个单元格链接到B列中的相应行
Sub FillData()
Dim rng1 As Range
Set rng1 = Range([a1], [a1].End(xlDown))
If Not (rng1.Rows.Count = Rows.Count And Len([a1].Value) = 0) Then rng1.Offset(0, 3).FormulaR1C1 = "=RC2"
End Sub
I like Sid's beginning, but once you have the range of rows, you can insert the formula into column D all at once, without looping, several ways, here's one: 我喜欢Sid的开头,但是一旦你有了行的范围,你可以一次性将公式插入到D列中,而不需要循环,有几种方法,这里是一个:
Option Explicit
Sub AddFormula()
Dim LR As Long
LR = Range("A" & Row.Count).End(xlUp).Row
Range("D2:D12").Formula = "=A2 + 7" 'just an example of a formula
End Sub
尝试这个:
Range("A:A").SpecialCells(2).Areas(1).Offset(, 3).Formula = "MyFormula"
This is a simple solution that is built into Excel, as long as you don't want to copy to the first blank, jump over the blank, then continue copying: 这是一个内置于Excel中的简单解决方案,只要您不想复制到第一个空白,跳过空白,然后继续复制:
Enter the formula in the first cell of your range, and as long as it is in the column directly to the right or left of your range of filled cells, simply double-click the black box handler in the bottom right-hand corner of the cell. 在范围的第一个单元格中输入公式,只要它在您填充单元格范围的右侧或左侧的列中,只需双击右下角的黑盒子处理程序即可。细胞。 That will automatically copy your formula down to the last non-empty cell of the range. 这将自动将您的公式复制到该范围的最后一个非空单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.