用循环过滤列中的值?
我正在尝试编写一个简单的循环来运行用户选择的值并过滤掉一些数字显示和我想要在不同列中的数字。 我在一列中有 10 个随机数,按以下顺序:
3
7
10
12
5
2
7
13
9
23
我本质上想忽略第一个值,检索接下来的两个值,跳过第四个值 值,检索接下来的两个值等等。所以我的输出是:
7
10
5
2
13
9
我所拥有的只是一个贯穿该列的循环。我想我必须使用 mod() 函数,但我无法做到正确。到目前为止我所拥有的只是这个空循环:
Sub findValues()
Do While Cells(x, 3).Value <> "" 'go through values in column 3 until empty cell is encountered
'skip first value
'retrieve next two values and put them in different column
'...
Loop
End Sub
I am trying to program a simple loop to run through the user selected values and filter out some number display and the numbers I want in a different column.
I have 10 random numbers in a column in this order:
3
7
10
12
5
2
7
13
9
23
I essentially want to ignore the first value, retrieve the next two values, skip the fourth
value, retrieve the next two values and so on. So my output would be:
7
10
5
2
13
9
All I have is a loop that runs through the column. I think I would have to use the mod() function but I can't sem to get it right. All I have so far is this empty loop:
Sub findValues()
Do While Cells(x, 3).Value <> "" 'go through values in column 3 until empty cell is encountered
'skip first value
'retrieve next two values and put them in different column
'...
Loop
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用循环和步骤 3 来完成此
操作的一种解决方案。这不是最快或优化的方法,但它是众多有效方法之一,而且该方法相当简单。该示例假设数据位于 A 列中,新列表将输出到 B 列。
由于您想跳过第一个值,因此我在 A2 处开始循环,然后每个循环执行步骤 3(但复制 2 个元素) ,所以它总是会跳过第三个元素)。
请注意,使用 Cells(row, column) 比 Range() 表示法更适合循环(而且速度也更快)。
Here's one solution to do it using a loop and Step 3.
It's not the fastest or optimized way, but it's one of many methods that works and this method is rather simplistic. The example assumes that the data is in column A and the new list will be output to column B.
Since you want to skip the first value, I start the loop at A2, then do a Step 3 each loop (but copy over 2 elements, so it'll always skip the 3rd element).
Note that using Cells(row, column) is better for looping than Range() notation (and faster, too).
[更新:公式解决方案]
在 D1 中输入此内容并向下复制 C 列长度的 2/3
=IF(MOD(ROW(),2)=1,偏移量($C$1,INT(ROW()/2)*3+1,0),偏移量($C$1,INT(ROW()/2) *3-1,0))
[初始帖子]
我使用了变体数组,因为它比循环更有效(尽管稍微复杂一些)
简而言之,您正在寻找的是 Mod 函数在哪里
(第 1 行)模 3 = 0
应该被排除,
即排除位置 1,4,7 等
此代码将输出从 C 列转储到 D。它将满足 C 中存在的尽可能多的值(注意我通过从底部向上查找而不是从下向上查找来设置 c 范围)顶部,因此代码中不会出现空白)
[Update: formula solution]
enter this in D1 and copy down 2/3 the length of your C column
=IF(MOD(ROW(),2)=1,OFFSET($C$1,INT(ROW()/2)*3+1,0),OFFSET($C$1,INT(ROW()/2)*3-1,0))
[initial post]
I've used a variant array as its more efficient (albeit slightly more complex) than a loop
In short what you were looking for is a Mod function where
(Row-1) Mod 3 = 0
should be excluded
ie exclude position 1,4,7 etc
This code dumps the output from column C to D. It will cater for as many values that exist in C (note I have set the c range by looking up from bottom not down from top, so blanks wont through the code out)