2017-03-27 8 views
2

金曜日に私のVBAコードを修正するのに役立つアドバイスをした後、私は同じようなユーザー定義関数で自分の手を試してみると思った。ここでのアイデアは、値リストと(オプションで)テーブル参照(例: "t")を使って、次のような文字列で終わることです。 t.value1 + t.value2 + t.value3Excel UDF - 取得VALUE!エラーと理由がわからない

私はタイプミスや間違った名前についてチェックしました(それでも可能性はありますが、何かが見逃されている可能性があります)。私がワークシートでそれを使用しようとすると、私は "VALUE!"エラー。以下のコードはExcelのVBAエディタ内のモジュールに保存されています。

ご意見ありがとうございます。

- あなただけのエラー処理を少し欠けている

'Here we'll create the formula's structure - these are the bits the worksheet user will choose: 
    Function ConcatenateToAdd(ConcatenateRange as Range, Optional TableReference as String = "") As Variant 'the default value for TableReference will be "" 

    'And here are our other building blocks that we'll use behind the scenes: 

     Dim i As Long 

     Dim strResult1 As String 'this will be everything up to the last value 
     Dim strResult2 As String 'this will add the last value on to the string produced as strResult1 

     Dim Separator1 As String 
     Dim Separator2 As String 

     Separator1 = "."   'this will slip between the table reference and the field name 
     Separator2 = " + "   'this will go after each field name, except the last. 



    'Just in case - let's make a back-up plan 
     On Error GoTo ErrHandler 

    'OK - let's go! 

    'First, let's string together every value but the last one: 
     For i = 1 To ConcatenateRange.Count - 1           
        strResult1 = strResult1 & TableReference & Separator1 & ConcatenateRange.Cells(i).Value & Separator2 

     Next i 


    'Lovely! Now let's just add on the last one - this one won't have a + on the end. 

     For i = ConcatenateRange.Count - 0 To ConcatenateRange.Count + 0      'I'm sure this is not the most elegant way to phrase this... 

      strResult2 = strResult1 & TableReference & Separator1 & ConcatenateRange.Cells(i).Value 

     Next I 

'The next bit tells Excel what the final result of the formula should be, in the worksheet: 
ConcatenateToAdd = strResult2 


    'And this is what the error handler does - it will just make Excel shout "ERROR!" at you. Let's hope it doesn't need to. 
      ErrHandler: 
     ConcatenateToAdd = CVErr(xlErrValue) 

    'And that's all!  
     End Function 
+1

実現しました。私は少しのコードを見逃しましたが、それを試してみたところ、違いはありません。見つからなかったビットは、ConcatenateToAdd = strResult2でした。私は今私の質問に追加しました。誰かが既に私が最初に投稿したコードを準備している場合に備えて説明したかっただけです。 –

答えて

2

(!P.S.の言い訳は、私のスタイルのコメント「VBAダミーのために、」私は午前 VBAのダミーためです)。 あなたのコードでは、あなたがいずれかのため、結果はエラー値に設定されますが起こるものは何でも:あなたはConcatenateToAdd = strResult2、または

Bを設定した後

a)の機能を終了していない)、エラーが実際に発生したことを確認しますErrHandler

で以下のようにそれを試してみてください - あなたは二つのループを必要とする(したがってのみstrResult1が必要)していないように私はあなたのコードを少しリファクタリングしました:

Option Explicit 

Function ConcatenateToAdd(ConcatenateRange As Range, Optional TableReference As String = "") As Variant 

    On Error GoTo ErrHandler 

    Dim i As Long 
    Dim strResult1 As String 
    Dim Separator1 As String 
    Dim Separator2 As String 

    ' update format if TableReference = "" 
    If TableReference = "" Then 
     Separator1 = "" 
    Else 
     Separator1 = "." 
    End If 
    Separator2 = " + " 

    strResult1 = "" 
    For i = 1 To ConcatenateRange.Count 
     strResult1 = strResult1 & TableReference & Separator1 & ConcatenateRange.Cells(i).Value 
     If i < ConcatenateRange.Count Then 
      strResult1 = strResult1 & Separator2 
     End If 
    Next i 

    ConcatenateToAdd = strResult1 

    'you could do an Exit Function here 
    'Exit Function 

' or continue into the ErrHandler block 
ErrHandler: 
    ' check an error actually occurred 
    If Err.Number <> 0 Then 
     ConcatenateToAdd = CVErr(xlErrValue) 
    End If 

    ' ConcatenateToAdd still equals strResult1 if no error occurred 

End Function 

注意すべきビットは、関数の戻り値は、文字列を作成した後に設定されていることである。

ConcatenateToAdd = strResult1 

あなたが次の行としてExit Functionを行うことができますが、ErrHandlerブロックに実行スリップを許可すれば、あなたがすべきのエラーが発生した場合は、ConcatenateToAddの値のみを更新してください。あなたはそれを次のように扱うことができます:

If Err.Number <> 0 Then 
    ConcatenateToAdd = CVErr(xlErrValue) 
End If 
+1

'TableReference =" "'なら 'Separator1 =" "'となるようなものを追加します。テーブルがない場合は '.'は不要です。 – CLR

+0

ありがとう、ロビン - それは治療を働いている!はい、私は最後のUDFでExit Functionを使用しましたが、今回は完全にそれを逃していました。私は木材だけでなく、木(または木のすべての木?)を見るのを助けてくれてありがとう! –

+0

うん、@ CLR - あまりにもそれを見つけた!私はロジックとセパレータについて練習します...(私がセパレータの値を設定した)セクションの周りにIfステートメントをラップすることはできますか? –

関連する問題