vba 如何根据不同单元格中的值自动填充特定行的单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23440270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:59:03  来源:igfitidea点击:

How to Autofill cells up to a certain row based on the value in a different cell?

excelexcel-vbacellautofillvba

提问by user3598259

So I have a cell (let's say C3)and I have a column in A.

所以我有一个单元格(let's say C3),我在 A 中有一个列。

Let's say that the formula I want to fill for the cells in columnA is A2 = A1 + 1and so forth. I have the number 30in C3.

假设我要为列中的单元格填充的公式A is A2 = A1 + 1等等。我有数量30C3

I want excel to fill exactly 30 cells down column A using that previous formula (A2 = A1 + 1). If I change the number in C3to 50, then excel will automatically add 20 more rows in column A following the same pattern.

我希望 excel 使用先前的公式填充 A 列下的 30 个单元格(A2 = A1 + 1)。如果我将数字更改C350,则 excel 将按照相同的模式在 A 列中自动添加 20 多行。

If I change the number in C3 to10, excel will automatically delete the rows until the first 10 rows remain.

如果我更改 中的数字C3 to10,excel 将自动删除行,直到保留前 10 行。

EDIT TO ADD: OK so I guess I have to use macros. Anyone suggest the VBA code for this?

编辑添加:好的,所以我想我必须使用宏。有人为此建议VBA代码吗?

回答by Nicholas Flees

You can avoid VBA if you know a maximum possible number of rows. Use the following formula in A2and apply it downward until you've applied through the maximum number of columns.

如果您知道最大可能的行数,则可以避免使用 VBA。使用以下公式A2并将其向下应用,直到应用到最大列数。

=IF(ROW()<=$C,A1+1,"")

So in reality, you still have a formula in these cells, but you won't see any output until they are supposed to be seen. It's a work-around.

所以实际上,您在这些单元格中仍然有一个公式,但是在应该看到它们之前您不会看到任何输出。这是一个变通办法。