2016-09-15 85 views
0

我希望它从上到下开始搜索第一列,然后是第二个等等。更改从左到右,从上到下,到上下,从左到右的搜索流程

在“每个”部分我想改变流动

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 

      'that is the Matrix I want to Change the search flow in' 
      For Each cell In Range("a2:aap15") 

       If cell.Interior.ColorIndex = 6 Then 
        If cell.Value = "" Then 

         cell.Value = ActiveCell.Value 
         Exit For 

        End If 
       End If 

      Next  
     Next  
    Next 

End Sub 

我必须写这句话,因为stackoverlow不会让我发布,直到它认为我没有做主要的代码了。

新建议(还是同样的问题):

Private Sub CommandButton1_Click() 
Dim i As Integer 
Dim j As Integer 

'NEW' 
Dim cell As Range, column As Range 

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

If Cells(i - 1, j).Value = 0 Then 
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 
End If 


'NEW' 
For Each column In Range("a2:aap15").Columns 
    For Each cell In column.Cells 


If cell.Interior.ColorIndex = 6 Then 
If cell.Value = "" Then 

cell.Value = ActiveCell.Value 

Exit For 

End If 
End If 

Next 
Next 
Next 
next 

End Sub 

how it shold be

how it is

+0

你真的有第一行中的所有东西(甚至是单个值)? .AutoFilter由于第一行空白而与您的示例数据搞砸了。 – Jeeped

+0

不,我的第一行是空的。为什么? – Julian

+0

您想使用'a2:aap15',但.AutoFilter需要'标题'行,第1行是空白。行1中任何单元格中的单个值都可以解决此问题。 – Jeeped

回答

0

您可以通过迭代的范围列的单元实现这一目标。


Dim cell As Range, column As Range 

For Each column In Range("a2:aap15").Columns 
    For Each cell In column.Cells 

     If cell.Interior.ColorIndex = 6 Then 
      If cell.Value = "" Then 

       cell.Value = ActiveCell.Value 
       Exit For 

      End If 
     End If 
    Next 
Next 
+0

如果使用我的整个代码,您的更正仍然会导致相同的流程。 – Julian

+0

我看不出如何。对范围使用'For Each'循环会导致行在列之前迭代。我的答案迭代列和行。 – 2016-09-15 11:46:12

+0

那么,我不知道如何。 – Julian

0

以下依赖于AutoFilter method。 .AutoFilter需要一个不包含在处理中的“标题”行,但行1不能完全空白。

Option Explicit 

Sub firstBlankYellows() 
    Dim c As Long, str As String 

    str = "Task 1" 
    With Worksheets("Sheet4") 
     If .AutoFilterMode Then .AutoFilterMode = False 
     With .Range("A1:AP15") 
      For c = 1 To .Columns.Count 
       With .Columns(c) 
        .AutoFilter field:=1, Criteria1:=RGB(255, 255, 0), Operator:=xlFilterCellColor 
        With .Resize(.Rows.Count - 1, 1).Offset(1, 0) 
         If Not Intersect(.SpecialCells(xlCellTypeVisible), _ 
             .SpecialCells(xlCellTypeBlanks)) Is Nothing Then 
          Intersect(.SpecialCells(xlCellTypeVisible), _ 
             .SpecialCells(xlCellTypeBlanks))(1) = str 
          Exit For 
         End If 
        End With 
        .AutoFilter 
       End With 
      Next c 
     End With 
     If .AutoFilterMode Then .AutoFilterMode = False 
    End With 
End Sub 

enter image description here

+0

如果我输入该代码,它会显示“在程序中无效” – Julian

+0

我已对上述内容进行了修改。 – Jeeped

+0

现在它找到显式选项的问题 – Julian

相关问题