2016-09-15 57 views
0

我的代码将矩阵1中的单元格中的文本复制到符合Matrix 2中的条件的所有单元格中。但是,我希望它仅将其复制到第一个单元格中在Matrix 2中满足我的批​​评,然后停下来。仅选择符合条件的范围中的第一个单元格

Private Sub CommandButton1_Click() 

Dim i As Integer 
Dim j As Integer 

For j = 2 To 2 
    For i = 21 To 21 

     If Cells(i, j).Value > 0 Then 
      Cells(i, j).Value = Cells(i, j).Value - 1 
      Cells(i, j).Offset(0, -1).Select 
     End If 

     'as it says - for EACH - so it copies in aLL the cells' 
     'I can't Change the range though, cause there will come a Loop eventually' 
     For Each cell In Range("a1:aap15") 

      If cell.Interior.ColorIndex = 6 Then 
       If cell.Value = "" Then 
        cell.Value = ActiveCell.Value 
       End If 
      End If 
     Next 
    Next 
Next 

End Sub 
+0

为什么你有第二个循环? '对于我= 21到21'?目的是什么?(或第一个?) –

+0

我会将b21的值减小到0,然后我会继续b22等等。我还没有到那个部分,所以我忽略了这个复杂因素,并且让它变得更加容易,直到我找出了我已经拥有的这些程序。 – Julian

回答

0

您可以使用Exit For命令来退出for循环。它看起来像你想在这里添加它:

If cell.Interior.ColorIndex = 6 Then 
    If cell.Value = "" Then 
     cell.Value = ActiveCell.Value 
     Exit For 
    End If 
End If 

注:未测试。让我知道如果你有任何问题

+0

它完美的作品。谢谢。 – Julian

+0

我刚刚意识到Excel在从左到右的范围内搜索 - 然后从上到下搜索。我可以使它从上到下搜索第一列,第二列等?我应该为此打开一个新问题吗? – Julian

+0

我会检查问题是否已经存在,并且已经在这里得到解答,或者您是否可以通过Google搜索找到答案。如果失败了,那么值得在一个新问题中提出 – CallumDA

相关问题