2017-03-31 1 views
2

Excel VBAで日付と倍数の多次元配列を返すUDFを作成しました。問題は、返される日付を書式設定できないことです。ここでユーザー定義関数がフォーマットできない日付を返すExcel VBA

は簡単な例です:最後に

Function test(dates as Range) 
    Dim results 
    Dim i As Integer 
    ReDim results(1 To dates.Cells.Count) 
    For i = 1 To dates.Cells.Count 
     results(i) = dates.Cells(i).Value 
    Next 
    test = Application.WorksheetFunction.Transpose(results) 
End Function 

転置(私はCtrlキー+ Shiftキーを押し+入力)だけ列出力を持っている利便性のためです。私はこの簡単な例を使用します。出力をフォーマットすることはできず、日付として扱われません。

アイデア?

+0

あなたのコードは、ちょうどあなたの出力セルを日付としてフォーマットされているか、一般的な* – CallumDA

+0

@CallumDAが私のためにそれが –

+0

@ScottCranerを再フォーマットすることができない文字列を返す*確認して私のために正常に動作します:OIが... – CallumDA

答えて

2

変更ダブルスに結果の配列:

Function test(dates As Range) 
    Dim results() As Double 
    Dim i As Integer 
    ReDim results(1 To dates.Cells.Count) 
    For i = 1 To dates.Cells.Count 
     results(i) = dates.Cells(i).Value 
    Next 
    test = Application.WorksheetFunction.Transpose(results) 
End Function 

またはDOUBLEない日付文字列が返されますどのdates.Cells(i).Value2dates.Cells(i).Valueを変更します。あなたが望むように、細胞をフォーマットすると

Function test(dates As Range) 
    Dim results 
    Dim i As Integer 
    ReDim results(1 To dates.Cells.Count) 
    For i = 1 To dates.Cells.Count 
     results(i) = dates.Cells(i).Value2 
    Next 
    test = Application.WorksheetFunction.Transpose(results) 
End Function 

を。

+0

今見OK、ありがとう。私はValue2を使用し、それが私の問題を解決しました! – qbodart

0

あなたはこのようなものを試すことができます。

Results(i) = CDate(dates.Cells(i).Value) 
関連する問題