简体   繁体   English

循环粘贴公式直到范围中的下一个单元格为空

[英]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.

相关问题 VBA值粘贴到范围中的下一个空单元格 - VBA Paste Value to Next Empty Cell in a Range 在下一个空行中复制并粘贴设置范围_LOOP - Copy and Paste a set range in the next empty row_LOOP 如果有数据,有没有办法遍历一个范围并在它旁边的单元格中返回一个公式? - Is there a way to loop through a range and return a formula in the cell next to it if there is data? 复制并粘贴到下一个空单元格 - Copy & Paste to the next empty cell 如何遍历范围直到单元格为空在Excel VBA中 - How to loop through a range until a cell is empty In Excel VBA VBA 循环 - 将单元格复制并粘贴到下一列,直到单元格 x 等于单元格 y - VBA Loop - copy and paste cells into next column until cell x equals cell y 如果表格范围内的单元格值等于“新建”,则复制整行并粘贴为工作表 1 中下一个空单元格中的值 - If cell value in table range equals “New” copy entire row and paste as values in sheet 1 in the next empty cell 从一个单元格复制/粘贴公式到另一列的18行中,从下一个空行开始 - Copy/Paste formula from a cell into 18 rows of another column starting on next empty row Excel宏循环遍历范围直到找到值,用公式填充找到的单元格下方的范围 - Excel macro to loop through range until value found, populate range below found cell with formula 从工作表 1 复制范围并粘贴到循环中下一张工作表的下一个空行 - Copy a range from sheet 1 and paste to next empty row on next sheets in loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM