2016-02-07 61 views
1

如果我有一個數據集是5x5。偏移到anthor列並在條件滿足時停止

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轉移到另一行。

誰能告訴我該怎麼做?

thx。

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

回答

3

而不是通過所有的細胞循環的,你可以找到你在找什麼。從你提供的代碼看,你的數據看起來像是從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 
2

試試這個

Sub macro1() 
For Each cell In Range("A1:E5") 
    If cell.Value = 1 Then 
     cell.Select 
     Exit For 
    End If 
Next cell 
End Sub