0

セル内で数式として使用され、問題が発生したように見える関数の作成を練習しています。以下のコードは、セルで使用されているときに文字列を返さない限り機能します。このコードは整数に対して完全に機能し、関数を使用するようにサブをコード化した場合は文字列に対しても機能します。セルの数式で使用すると、なぜ返されるのか理解できないようです。

Function bIsBlank(x As String) As String

Dim y As String

If x = "" Then  'check to see if it is blank
    Exit Function
Else 
    y = Evaluate(x) 'solve the formula that is pulled in

    If y = "0" Then 'check to see if formula equals zero
        y = ""      'set to nothing if it is zero
    End If
End If

    'below is my attempt to fix it by adding quotes on either side of the string
    'this did not work
    y = Chr(34) & y & Chr(34)

'this seems to solve for a sub but not a formula in a cell for strings
bIsBlank = y

End Function

問題は、evaluate関数を使用する必要がなかったことです。私が引き込んでいた式はすでに解決されています。つまり、「A」を解決するvlookupがあったとします。コードX="A"では、当初の予想どおり、文字列vlookup(a1、a5:b10,2,0)ではありません。

4

2 に答える 2

0

1.常に使用するOption Explicit

2. Stringは値型であり、参照することはできませんNull

于 2013-01-28T18:28:44.270 に答える
0

これはあなたがしようとしていることですか?

Option Explicit

Function bIsBlank(Optional x As String) As String
    Dim y As String

    bIsBlank = ""

    If x = "" Then Exit Function

    y = Application.Evaluate(x)

    If y = 0 Then bIsBlank = "" Else _
    bIsBlank = y
End Function

さまざまなシナリオ

ここに画像の説明を入力

ここに画像の説明を入力

:数式を入力してセルの値を変更するとA1:A10、 はbIsBlank()自動的に計算されません。そのために使わなければなりませんApplication.Volatile。以下の例を参照してください

Option Explicit

Function bIsBlank(Optional x As String) As String
    Dim y As String

    Application.Volatile

    bIsBlank = ""

    If x = "" Then Exit Function

    y = Application.Evaluate(x)

    If y = 0 Then bIsBlank = "" Else _
    bIsBlank = y
End Function
于 2013-01-29T07:12:09.380 に答える