我有一张看起来像这样的表:
--------------------------------------------------
| cat | cat | cat | dog | bird | dog | dog | dog |
--------------------------------------------------
| | | | | | | | |
--------------------------------------------------
我创建了下面的内容,它读取上面的行并在值存在时跳过,如果不存在则输入数据。除了在上面的行中有两个或更多要跳过的值时,这种方法效果很好。
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 |
--------------------------------------------------
但相反它正在这样做 - 我相信因为它说,是的猫意味着,下一个col所以我会添加一个是(不再检查它的col):
--------------------------------------------------
| cat | cat | cat | dog | bird | dog | dog | dog |
--------------------------------------------------
| | yes | | yes | | yes | yes | yes |
--------------------------------------------------
答案 0 :(得分:2)
您可以在数组中添加要比较的所有字符串,然后检查数组中是否存在单元格值,并相应地执行进一步的操作。
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
答案 1 :(得分:1)
你的程序完全按照你的要求去做
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)