简体   繁体   English

向下填充公式直到列中的最后一行

[英]Fill formula down till last row in column

I'm trying to draw down the formula that's in cell M3 to the end of the data set.我正在尝试将单元格 M3 中的公式绘制到数据集的末尾。

I'm using column L as my base to determine the last cell with data.我使用 L 列作为我的基础来确定最后一个包含数据的单元格。 My formula is a concatenation of two cells with a text comma in-between them.我的公式是两个单元格的串联,它们之间有一个文本逗号。

My formula is =G3&","&L3我的公式是 =G3&","&L3

I want Excel to draw down this formula so我要 Excel 写下这个公式

Cell M4 would be =G4&","&L4单元格 M4 将是 =G4&","&L4
Cell M5 would be =G5&","&L5 and so on.单元格 M5 将是 =G5&","&L5 等等。

My code:我的代码:

Range("$M$3").Formula = Range("G3") & (",") & Range("L3")

Dim Lastrow As Long

Application.ScreenUpdating = False

Lastrow = Range("L" & Rows.Count).End(xlUp).Row
Range("M4").FormulaR1C1 = Range("G4") & (",") & Range("L4")
Range("M4").AutoFill Destination:=Range("M4:M" & Lastrow)
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True

My output is pulling down the text values from cell M3 all the way down to the end of the data set.我的 output 正在将文本值从单元格 M3 一直下拉到数据集的末尾。 I've searched around for several hours trying to look for a fix, but can't find one that is trying to accomplish what I'm going for.我已经搜索了几个小时试图寻找修复,但找不到一个正在尝试完成我想要的东西。

It's a one liner actually. 实际上是一个班轮。 No need to use .Autofill 无需使用。 .Autofill

Range("M3:M" & LastRow).Formula = "=G3&"",""&L3"

For people with a similar question and find this post (like I did); 对于有类似问题的人,可以找到此职位(就像我一样); you can do this even without lastrow if your dataset is formatted as a table. 如果您的数据集被格式化为表格,那么即使没有lastrow,您也可以执行此操作。

Range("tablename[columnname]").Formula = "=G3&"",""&L3"

Making it a true one liner. 使它成为真正的一线。 Hope it helps someone! 希望它能对某人有所帮助!

或者,您可以使用FillDown

Range("M3") = "=G3&"",""&L3": Range("M3:M" & LastRow).FillDown

Wonderful answer! 很棒的答案! I needed to fill in the empty cells in a column where there were titles in cells that applied to the empty cells below until the next title cell. 我需要填写一列中的空白单元格,在该单元格中有适用于以下空白单元格的标题,直到下一个标题单元格为止。

I used your code above to develop the code that is below my example sheet here. 我在上面的示例代码中使用了上面的代码来开发代码。 I applied this code as a macro ctl/shft/D to rapidly run down the column copying the titles. 我将此代码用作宏ctl / shft / D来快速运行复制标题的列。

--- Example Spreadsheet ------------ Title1 is copied to rows 2 and 3; ---电子表格示例------------ Title1复制到第2行和第3行; Title2 is copied to cells below it in rows 5 and 6. After the second run of the Macro the active cell is the Title3 cell. 将Title2复制到第5和6行中位于其下方的单元格中。在第二次运行宏后,活动单元格为Title3单元格。

 ' **row** **Column1**        **Column2**
 '    1     Title1         Data 1 for title 1
 '    2                    Data 2 for title 1
 '    3                    Data 3 for title 1
 '    4     Title2         Data 1 for title 2
 '    5                    Data 2 for title 2
 '    6                    Data 3 for title 2
 '    7   Title 3          Data 1 for title 3

----- CopyDown code ---------- ----- CopyDown代码----------

Sub CopyDown()
Dim Lastrow As String, FirstRow As String, strtCell As Range
'
' CopyDown Macro
' Copies the current cell to any empty cells below it.   
'
' Keyboard Shortcut: Ctrl+Shift+D
'
    Set strtCell = ActiveCell
    FirstRow = strtCell.Address
' Lastrow is address of the *list* of empty cells
    Lastrow = Range(Selection, Selection.End(xlDown).Offset(-1, 0)).Address
'   MsgBox Lastrow
    Range(Lastrow).Formula = strtCell.Formula

    Range(Lastrow).End(xlDown).Select
 End Sub

另一种方法是简单地确保在 'Files'-> 'Options'->'Formulas'->'Workbook Calculation'->'Automatic' 中自动计算或在执行操作之前放置这行代码:

    Application.Calculation = xlAutomatic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM