vba - 偏移到另一列并在满足条件时停止

标签 vba excel

如果我有一个数据集是 5x5。

0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 1

我想将我的选择移动到单元格为 1。

我写了一个宏,但我只能要求它在单元格为 1 时停止。

当 5 列通过后,我不知道如何告诉 excel 转移到另一行。

谁能告诉我该怎么做?

谢谢。
Sub macro1()
'
Range("A2").Select
Do
ActiveCell.Offset(0, 1).Select
Loop Until ActiveCell.Value = 1
End Sub

最佳答案

您可以找到您要查找的内容,而不是遍历所有单元格。从您提供的代码来看,您的数据似乎从 A2 开始。

Sub Find_One()
    Dim rng As Range, oC As Range, s As String
    s = 1    ' this is what you want to find
    Set rng = Range("A2:E6")

    Set oC = rng.Find(what:=s, lookat:=xlWhole)

    If Not oC Is Nothing Then
        Cells(oC.Row, oC.Column).Select
    Else: MsgBox "Not Found"
        Exit Sub
    End If

End Sub

如果“1”将多次出现,那么此代码将适合您。
Sub Select_1()
    Dim FrstRng As Range
    Dim UnionRng As Range
    Dim c As Range

    Set FrstRng = Range("A2:E6")

    For Each c In FrstRng.Cells

        If c = 1 Then
            If Not UnionRng Is Nothing Then
                Set UnionRng = Union(UnionRng, c)    'adds to the range
            Else
                Set UnionRng = c
            End If
        End If

    Next c

    UnionRng.Select

End Sub

关于vba - 偏移到另一列并在满足条件时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251755/

相关文章:

excel - 在 VBA Excel 2010 中选择事件行和第一行

vba - 列表框下标超出范围excel vba

excel - 将文件名中具有不同日期的附件添加到 Outlook 邮件

java - 无法将数据追加到现有 Excel 中

excel - 字符串如何存储在 VBA 字典结构中?

excel - 无法使用 MATCH 函数和 VBA 匹配范围内的日期

vba - 删除工作表中的所有内容

sql - 使用 ADODB 连接从关闭的工作簿中检索数据。某些数据被跳过?

excel - 这个工作簿在 Excel 2018 中不起作用?

excel - PowerPoint 链接对象的新驱动器映射 - VBA 解决方案