Excel – 循环问题

我有一张如下所示的表格:

-------------------------------------------------- | cat | cat | cat | dog | bird | dog | dog | dog | -------------------------------------------------- | | | | | | | | | -------------------------------------------------- 

我创build了下面的代码,读取上面的行,如果存在值,则跳过或input不存在的数据。 除了在上面的行中有两个或多个值要跳过的情况之外,这种方式非常有效。

 Dim i As Integer Dim c As Integer Dim t As Integer c = 5 t = 7 Dim animal As String For i = 1 To t ' this can be cat, dog or bird - from another sheet, this part works OK animal = Cells(3, c).Value ' this is the part that doesn't work, it should move to next cell if one of those values exists If InStr(1, animal , "cat") Or InStr(1, animal , "bird") Or Instr(1, animal, "duck") Then c = c + 1 End If Cells(4, c).Value = "yes" c = c + 1 Next i 

我已经尝试了所有似乎有不同的结果,但这是尽可能接近我已经能够得到它:(

这意味着要做到这一点:

  -------------------------------------------------- | cat | cat | cat | dog | bird | dog | dog | dog | -------------------------------------------------- | | | | yes | | yes | yes | yes | -------------------------------------------------- 

但相反,它是这样做的 – 我相信因为它是说,是的,猫意味着,下一个列,所以我会添加一个是的(不再检查它再次):

  -------------------------------------------------- | cat | cat | cat | dog | bird | dog | dog | dog | -------------------------------------------------- | | yes | | yes | | yes | yes | yes | -------------------------------------------------- 

您可以添加要在数组中进行比较的所有string,然后检查数组中是否存在单元格值,并相应地执行其他操作。

 Sub Demo() Dim lastColumn As Long, i As Long Dim animal As String Dim arr() arr = Array("cat", "bird", "duck") 'store all strings in array to match lastColumn = Cells(3, Columns.Count).End(xlToLeft).Column 'get last column with data in Row 3 For i = 5 To lastColumn 'loop through Row 3 starting from Column 5 animal = Cells(3, i).Value If IsError(Application.Match(animal, arr, False)) Then 'check if animal is in array Cells(4, i).Value = "yes" 'enter animal in Row 4 End If Next i End Sub 

编辑1:根据评论

 Sub Demo() Dim lastColumn As Long, i As Long Dim animal As String lastColumn = Cells(3, Columns.Count).End(xlToLeft).Column 'get last column with data in Row 3 For i = 5 To lastColumn 'loop through Row 3 starting from Column 5 animal = Cells(3, i).Value If InStr(1, animal, "cat") = 0 And InStr(1, animal, "bird") = 0 And InStr(1, animal, "duck") = 0 Then Cells(4, i).Value = "yes" 'enter animal in Row 4 End If Next i End Sub 

编辑2:

 Sub Demo() Dim lastColumn As Long, i As Long, t As Long Dim animal As String lastColumn = Cells(3, Columns.Count).End(xlToLeft).Column 'get last column with data in Row 3 t = 9 i = 5 'this should be the column number from where data starts Do While t > 0 animal = Cells(3, i).Value If InStr(1, animal, "cat") = 0 And InStr(1, animal, "bird") = 0 And InStr(1, animal, "duck") = 0 Then Cells(4, i).Value = "yes" t = t - 1 End If i = i + 1 Loop End Sub 

你的程序正在完成你所要做的事情

 your code flow: point to first column (1) is it cat/bird/duck? yes (cat) ... point to next column (2) put in "yes" point to next column (3) is it cat/bird/duck? yes (cat) ... point to next column (4) put in "yes" point to next column (5) is it cat/bird/duck? yes (bird)... point to next column (6) put in "yes" point to next column (7) is it cat/bird/duck? no put in "yes" point to next column (8) is it cat/bird/duck? no put in "yes" point to next column (9)