if语句,检查工作表中是否有两个字符串之一

时间:2018-02-21 17:04:57

标签: excel vba excel-vba

我坚持这个简单的if语句。如果字符串" AAA"不在表格上,而是字符串" BBB"是。代码不会转到代码的ElseIf行,但只会抛出错误。

  

运行时错误91:对象变量或未设置块

我尝试了很多不同的代码变体,我不会发布所有代码,因为它们都会给我一些错误。 :/

 echo "<td class='text-center'><a target='_blank'  class='btn btn-sm btn-
 info' href='edit.php?id=" . $value['id'] . "'> Vezi Comanda</a></td>\n";

忽略MsgBox旁边的=符号,MsgBox仅用于简化问题

5 个答案:

答案 0 :(得分:3)

您需要先检查是否有找到的范围对象。 您的MsgBox语法也不正确:

Dim R       As Range
Set R = ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("AAA")

If Not R Is Nothing Then
    VarID = "AAA"
    MsgBox VarID
Else
    Set R = ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("BBB")
    If Not R Is Nothing Then
        VarID = "BBB"
        MsgBox VarID
    End If
End If

答案 1 :(得分:2)

您可以使用两个范围变量:

Sub t()
Dim foundRange1 As Range, foundRange2 As Range
Dim varID As String
Set foundRange1 = ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("AAA")
Set foundRange2 = ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("BBB")
If Not foundRange1 Is Nothing Then ' If foundRange is *not* empty (Nothing), then it found the string.
    varID = "AAA"
    MsgBox varID
ElseIf Not foundRange2 Is Nothing Then
    varID = "BBB"
    MsgBox varID
End If
End Sub

或者,如果你有一堆字符串,你可以使用类似这样的循环:

Sub t2()
Dim strings() As Variant
Dim varID As String

strings = Array("AAA", "BBB", "CCC", "DDD")

Dim i As Long
i = 0

For i = LBound(strings) To UBound(strings)
    Do While Not ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find(strings(i)) Is Nothing
        varID = strings(i)
        MsgBox (varID)
        Exit For
    Loop
Next i

End Sub

答案 2 :(得分:1)

也许:

Sub qwerty()
If Not ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("AAA") Is Nothing Then

        VarID = "AAA"
        MsgBox VarID

    ElseIf Not ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("BBB") Is Nothing Then

        VarID = "BBB"
        MsgBox VarID

    End If
End Sub

答案 3 :(得分:1)

执行其他答案所表达的另一种方法是返回范围对象的class Product(models.Model): name = models.CharField(max_length=250, unique=True) quantity = models.IntegerField(default=0) class Order(models.Model): products = models.ManyToManyField( Product, through='PositionInOrder' ) discount = models.CharField(max_length=50, blank=True) total_sum = models.DecimalField(max_digits=11, decimal_places=2, default=0) class PositionInOrder(models.Model): sale = models.ForeignKey(Order) product = models.ForeignKey(Product) quantity = models.PositiveIntegerField() price = models.PositiveIntegerField() 函数的问题:

class ProductSerializer(serializers.ModelSerializer):

    class Meta:
        model = Product
        fields = '__all__'

class PositionInOrderSerializer(serializers.HyperlinkedModelSerializer):
    sale = serializers.ReadOnlyField()
    product = serializers.ReadOnlyField()

    class Meta:
        model = PositionInOrder
        fields = "__all__"

class OrderSerializer(serializers.ModelSerializer):
    products = PositionInOrderSerializer(many=True)

    def create(self, validated_data):
        products = validated_data.pop('products')
        order = Order.objects.create(**validated_data)
        return order

    class Meta:
        model = Order
        fields = '__all__'

答案 4 :(得分:1)

您只需将Variant声明为=并删除MsgBox中的Sub find() Dim VarID As Variant If ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("AAA") = "AAA" Then VarID = "AAA" MsgBox VarID ElseIf ActiveWorkbook.ActiveSheet.Range("A1:BZ999").Find("BBB") = "BBB" Then VarID = "BBB" MsgBox VarID End If End Sub

result[0]