用循环过滤列中的值?

发布于 2024-12-08 14:29:36 字数 550 浏览 0 评论 0原文

我正在尝试编写一个简单的循环来运行用户选择的值并过滤掉一些数字显示和我想要在不同列中的数字。 我在一列中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-12-15 14:29:41

这是使用循环和步骤 3 来完成此

操作的一种解决方案。这不是最快或优化的方法,但它是众多有效方法之一,而且该方法相当简单。该示例假设数据位于 A 列中,新列表将输出到 B 列。

由于您想跳过第一个值,因此我在 A2 处开始循环,然后每个循环执行步骤 3(但复制 2 个元素) ,所以它总是会跳过第三个元素)。

Sub test()

Application.ScreenUpdating = False
Dim i As Long, j As Long
Dim lastRow As Long

lastRow = Cells(Rows.Count, 1).End(xlUp).Row
j = 1

For i = 2 To lastRow Step 3
    Cells(j, 2).Value = Cells(i, 1).Value
    Cells(j + 1, 2).Value = Cells(i + 1, 1).Value
    j = j + 2
Next

Application.ScreenUpdating = True
End Sub

请注意,使用 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).

Sub test()

Application.ScreenUpdating = False
Dim i As Long, j As Long
Dim lastRow As Long

lastRow = Cells(Rows.Count, 1).End(xlUp).Row
j = 1

For i = 2 To lastRow Step 3
    Cells(j, 2).Value = Cells(i, 1).Value
    Cells(j + 1, 2).Value = Cells(i + 1, 1).Value
    j = j + 2
Next

Application.ScreenUpdating = True
End Sub

Note that using Cells(row, column) is better for looping than Range() notation (and faster, too).

狼性发作 2024-12-15 14:29:41

[更新:公式解决方案]

在 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 范围)顶部,因此代码中不会出现空白)

Sub GetValues()
        Dim rng1 As Range
        Dim lngCnt As Long
        Dim lngCnt2 As Long
        Dim X
        Dim Y
        Set rng1 = Range([c1], Cells(Rows.Count, "C").End(xlUp))
        X = rng1
        ReDim Y(1 To 2 / 3 * rng1.Cells.Count, 1 To 1)
        For lngCnt = 1 To UBound(X, 1)
            If (lngCnt - 1) Mod 3 <> 0 Then
            lngCnt2 = lngCnt2 + 1
            Y(lngCnt2, 1) = X(lngCnt, 1)
            End If
        Next
        [d1].Resize(UBound(Y, 1), 1) = Y
    End Sub]

在此处输入图像描述

[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)

Sub GetValues()
        Dim rng1 As Range
        Dim lngCnt As Long
        Dim lngCnt2 As Long
        Dim X
        Dim Y
        Set rng1 = Range([c1], Cells(Rows.Count, "C").End(xlUp))
        X = rng1
        ReDim Y(1 To 2 / 3 * rng1.Cells.Count, 1 To 1)
        For lngCnt = 1 To UBound(X, 1)
            If (lngCnt - 1) Mod 3 <> 0 Then
            lngCnt2 = lngCnt2 + 1
            Y(lngCnt2, 1) = X(lngCnt, 1)
            End If
        Next
        [d1].Resize(UBound(Y, 1), 1) = Y
    End Sub]

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文