[英]Excel Macro or Function: Populate a cell range (Column A) with a looped values (from another sheet) if cell is not blank
Need help for someone trying to learn macro coding. 需要帮助尝试学习宏编码的人。
Sheet 1 has two Columns A & B. Sheet 2, Col A has a range of value. 工作表1有两个A和B栏。工作表2的A列有一个值范围。 I wanted to fill Sheet 1 Column B with a value on Sheet 2 Column A if Sheet 1 Column A cell is not empty.
如果工作表1列A单元格不为空,我想用工作表2列A上的值填充工作表1列B。
Illustration below. 下图。
Sheet2 Col A
Brian,
David,
Nicole,
Aspen,
Col A
Apple
Banana
Orange
Lemon
Mango
Strawberry
Grape
Col B should get filled by looping with the names, Brian for Apple and then David for Banana, and so on and then go back to David and so on until Grape for Nicole.. 上校B的名称应该是循环的,Brian是Apple的名字,然后是David的Banana名字,依此类推,然后回到David,依此类推,直到Grape的名字是Nicole。
Please help provide with a function or macro vba code to accomplish this. 请帮助提供函数或宏vba代码以完成此操作。
One way is to use INDIRECT
: 一种方法是使用
INDIRECT
:
=INDIRECT("Sheet2!A" & MOD(ROW()+3,4)+1)
This assumes the names are in A1:A4
假设名称在
A1:A4
Edit: and I just thought of a way to use INDEX
instead: 编辑:我只是想了一种使用
INDEX
的方法:
=INDEX(Sheet2!A$1:A$4,MOD(ROW()+3,4)+1)
MOD
will return the remainder of the row number divided by n
(in this case, 4
). MOD
将返回行号的其余部分除以n
(在这种情况下为4
)。 This will result in a number between 0
and 3
( n-1
), inclusively. 这将导致数字介于
0
和3
( n-1
)之间(含0
和3
)。 the sequence for rows 1-6
is 1, 2, 3, 0, 1, 2
. 对于行序列
1-6
是1, 2, 3, 0, 1, 2
。
By adding 3
( n-1
) to the row number, I can get the sequence for rows 1-6
to become 0, 1, 2, 3, 0, 1
通过将
3
( n-1
)添加到行号,我可以获得1-6
行的序列变为0, 1, 2, 3, 0, 1
Then adding 1
to the mod makes the sequence: 1, 2, 3, 4, 1, 2
, which I can then use as the index for the table of names 然后在mod上加
1
即可形成序列: 1, 2, 3, 4, 1, 2
然后我可以将其用作名称表的索引
For 12 names you will want to use: 对于12个名称,您将要使用:
=INDEX(F$1:F$12,MOD(ROW()+11,12)+1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.