使用带有文本字符串

时间:2016-01-03 14:30:46

标签: excel vba excel-vba

我想问一下vba excel,我想填写多个单元格的公式,但公式包含文本字符串。

Sub Test()

    Dim strFormulas(1 To 2) As Variant

    With ActiveSheet
        strFormulas(1) = "=ISNUMBER(SEARCH(Apple,B2))"
        strFormulas(2) = "=ISNUMBER(SEARCH(Orange,B2))"

        .Range("C2:D2").Formula = strFormulas
        .Range("C2:D11").FillDown
    End With

End Sub

在第一个我这样做,但因为本地没有文本字符串,所以结果都是假的,因为公式应该是=ISNUMBER(SEARCH("Apple",B2))

2 个答案:

答案 0 :(得分:5)

strFormulas(1) = "=ISNUMBER(SEARCH(""Apple"",B2))"
strFormulas(2) = "=ISNUMBER(SEARCH(""Orange"",B2))"

答案 1 :(得分:2)

加倍引用是一个更简单的解决方案,但如果您感兴趣,另一种方法是使用Chr(34)

Sub Test()

    Dim strFormulas(1 To 2) As Variant

    With ActiveSheet
        strFormulas(1) = "=ISNUMBER(SEARCH(" & Chr(34) & "Apple" & Chr(34) & ",B2))"
        strFormulas(2) = "=ISNUMBER(SEARCH(" & Chr(34) & "Orange" & Chr(34) & ",B2))"

        .Range("C2:D2").Formula = strFormulas
        .Range("C2:D11").FillDown
    End With

End Sub