简体   繁体   English

将公式向下复制x行数

[英]Copying a formula down through x number of rows

I'm at a loss on this and need some help. 我对此感到茫然,需要一些帮助。 I've lurked around at answers and have Frankensteined together some code for a macro but it just isn't working. 我已经潜伏在答案中并且将Frankensteined合并为宏的一些代码,但它只是不起作用。

Here is part of what I have so far: 这是我到目前为止的一部分:

With ActiveSheet
Firstrow = 1
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For lrow = Lastrow To Firstrow Step -1
With .Cells(lrow, "G")
    Range("G1").Select
    ActiveCell.FormulaR1C1 = "=IF(ISNUMBER(RC[1]),RC[1],RC[-1])"
End With
Next lrow
End With

I have a very similar block of code before this that deletes crap from the text files I'm importing and it works perfectly through all the number of rows. 在此之前我有一个非常相似的代码块,它从我正在导入的文本文件中删除了废话,并且它在所有行数中都能正常工作。 When I run the same thing with this formula, it only puts the formula in G1 and doesn't cycle through the rest of the sheet. 当我使用这个公式运行相同的东西时,它只将公式放在G1中,并且不会循环通过表格的其余部分。 I've tried this and it works, but copies down through all million plus rows: 我已经尝试了这个并且它可以工作,但是通过所有百万行来复制:

ActiveCell.FormulaR1C1 = "=IF(ISNUMBER(RC[1]),RC[1],RC[-1])"
Selection.AutoFill Destination:=Range("G:G")

I've tried this and then run the same code that gets rid of the text file crap but I get an error "End If without block If". 我试过这个,然后运行相同的代码,摆脱文本文件废话,但我得到一个错误“结束如果没有阻止如果”。

To fill the formula in one cell at a time you need to cycle through them; 要一次填充一个单元格中的公式,您需要循环它们; don't keep relying on the ActiveCell property . 不要继续依赖ActiveCell属性

With ActiveSheet
    Firstrow = 1
    Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For lrow = Lastrow To Firstrow Step -1
        .Cells(lrow, "G").FormulaR1C1 = "=IF(ISNUMBER(RC[1]),RC[1],RC[-1])"
    Next lrow
End With

But you can speed things up by putting the formula into all of the cells at once. 但是你可以通过将公式同时放入所有单元格来加快速度。

With ActiveSheet
    Firstrow = 1
    Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    With .Range(.Cells(Firstrow, "G"), .Cells(Lastrow, "G"))
        .FormulaR1C1 = "=IF(ISNUMBER(RC[1]),RC[1],RC[-1])"
    End With
End With

See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals. 请参阅如何避免在Excel VBA宏中使用Select以获取更多方法, 以避免依赖select和activate来实现目标。

Another version, to dynamically select the columns based on their titles. 另一个版本,根据标题动态选择列。 Comments included. 包括评论。

Dim row As Range
Dim cell As Range
Static value As Integer

'Set row numbers

'Find the starting row. Located using Title of column "Start" plus whatever number of rows.
Dim RowStart As Long
Set FindRow = Range("A:A").Find(What:="Start", LookIn:=xlValues)
RowStart = FindRow.row + 1

'End of the range. Located using a "finished" cell
Dim RowFinish As Long
Set FindRow = Range("A:A").Find(What:="Finished", LookIn:=xlValues)
 RowFinish = FindRow.row - 1

'Set range - Goes Cells(Rownumber, Columnnumber)
'Simply ammend RowStart and RowFinish to change which rows you want.
' In your case you need to change the second column number to paste in horizontally.
Set rng = Range(Cells(RowStart, 1), Cells(RowFinish, 1))
'Start the counter from the starting row.
value = RowStart

For Each row In rng.Rows
  For Each cell In row.Cells

'Insert relevant formula into each cell in range.
cell.Formula = _
"=IF(ISNUMBER(RC[1]),RC[1],RC[-1])"

   'Increment row variable.
    value = value + 1
  Next cell
Next row

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

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