简体   繁体   English

使用Loop和If,ElseIf导致出现消息:“找不到”(我在寻找什么)”

[英]Using Loop and If, ElseIf resulting in message: “Couldn't find ”(what I am looking for)"

Please not that this is one of the first VBA code I have written. 请不要说这是我编写的第一个VBA代码之一。 What I wrote was a mixture of a loop and If function. 我写的是循环和If函数的混合体。 The code was supposed to go through column("AC"), if it finds an empty cell. 如果找到空单元格,则该代码应通过column(“ AC”)。 It looks at the same row but in column D. Column D would have 350 possible values. 它查找同一行,但在D列中。D列将具有350个可能的值。 If the value of Column D is "one of 350 possible values" then give "EU" ElseIf... this continues for all 350 如果D列的值为“ 350个可能的值之一”,则为“ EU”,否则...对于所有350个继续

Dim i As Long
i = 1
Do While Cells(i, "AC").Value <> ""
    If Cells(i, "D").Value = "OPP-1305-9066" Then
        Cells(i, "AC").Value = EU
    ElseIf Cells(i, "D").Value = "OPP-1305-0773" Then
        Cells(i, "AC").Value = EU
    ElseIf Cells(i, "D").Value = "another one of 350" Then
        Cells(i, "AC").Value = EU
    End If
    i = i + 1
Loop

Not sure what your questions is, but unless EU is a variable, you are missing brackets. 不知道您的问题是什么,但是除非EU是一个变量,否则您将缺少括号。 Also you only loop while column AC is not empty, so if AC2 is empty but there's more data after cell AC4 you're not checking it. 另外,您仅在AC列不为空时循环,因此,如果AC2为空,但是在单元格AC4之后有更多数据,则无需检查。

And most importantly, 350 elseif statements, wow! 最重要的是,还有350条elseif语句,哇! :) You will benefit from learning about arrays/collections. :)您将从学习数组/集合中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM