Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel-循环问题_Excel_Vba - Fatal编程技术网

Excel-循环问题

Excel-循环问题,excel,vba,Excel,Vba,我有一张这样的纸: -------------------------------------------------- | cat | cat | cat | dog | bird | dog | dog | dog | -------------------------------------------------- | | | | | | | | | -------------------------------

我有一张这样的纸:

 --------------------------------------------------
 | 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 |
 --------------------------------------------------
但实际上它是这样做的——我相信,因为它说的是,yes cat的意思是,下一个col,所以我将添加yes(不再检查它所在的col):


你的程序正在做你告诉它要做的事情

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)

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

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

我不明白-你想让它只为“狗”说“是”吗?@Vlad有点,它被修改为删除了很多信息,所以理解起来会更难理解-基本上最上面一行可能有狗1234,狗281,狗2899,猫02928,鸭9228,因此搜索猫,鸟,鸭。如果其中任何一个出现在最上面一行,我会使用nt命令将其移动到下一个单元格并粘贴到其中,但在粘贴时它没有检查上一行中的下一个单元格-这有意义吗?请理解,在没有多个嵌套If语句的情况下,如何让它执行我希望它执行的操作?@hylian-请参见答案中的编辑。根据您在问题中的注释更新代码。@hylian-该v在哪里value?@hylian-然后您可以编写
lastColumn=Range(“A1”).Value
replace
A1
到您的实际单元格。@hylian-如果我理解正确,即使
3行中的数据不存在,那么循环也应该继续,直到
yes
Value在
第4行中输入了7次。@hylian-如果我的上述注释正确,请参见回答中的
EDIT 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