2012-06-28 55 views
0

(該團體感謝的大力協助!)以下的偉大工程VBA UDF株/整數和株/字符串數組僅打印第一個值來輸出單元

Function RangeToArrayToRange(inputRange as Range) As Variant 
      Dim inputArray As Variant 
      inputArray = inputRange 
      RangeToArrayToRange = inputArray 
    End Function 

此功能將複製的輸入範圍到完美的輸出。但是,當我對inputArray執行一些操作時,數組看起來很完美,但在Excel中,只有數組的第一個值會打印到所有單元格。在這個例子中,我從一些輸入字符串中解析出一個數字。

輸入範圍:

ABC=1X:2Y 
ABCD=10X:20Y 
ABCDE=100X:200Y 

代碼:

Function RangeToArrayToRange(inputRange As Range) As Variant 

     Dim inputHeight As Integer 
     inputHeight = inputRange.Count 

     Dim inputArray As Variant 
     inputArray = inputRange 

     Dim strippedArray() As Variant 
     ReDim strippedArray(1 To inputHeight) 

     Dim currentInput As String 
     Dim currentInputAsInt As Integer 
     Dim i As Integer 

     For i = 1 To inputHeight 

      currentInput = inputArray(i, 1) 
      currentInput = Right(currentInput, (Len(currentInput) - Application.WorksheetFunction.Find("=", currentInput))) 
      'splits out everything left of the "=" 
      currentInput = Right(currentInput, (Len(currentInput) - Application.WorksheetFunction.Find(":", currentInput))) 
      'splits out everything to the right of the ":" 
      currentInput = Left(currentInput, Len(currentInput) - 1) 
      'split out the letter to allow int casting 
      currentInputAsInt = CInt(currentInput) 
      'cast to int 
      strippedArray(i) = currentInputAsInt 
      'saved 

     Next i 

     RangeToArrayToRange = strippedArray 
    End Function 

預期輸出:

1 
10 
100 

實際輸出:

1 
1 
1 

strippedArray在調試器中運行,分別在位置strippedArray(1)/(2)/(3)中包含Variant/Integer值1,10,100。問題是我在Excel中輸入的範圍只包含strippedArray(1),據我所知。

謝謝!

+0

所以你說該數組是否正確填寫,但只有第一個值打印到所有單元格?但是,您打印的代碼在哪裏?我只看到一個返回數組的函數... – Trace

回答

4

您的strippedArray數組需要是二維的,如果您要輸出回Excel工作表/範圍(我已經假設您將此數組公式運行)。做以下修改:

ReDim strippedArray(1 To inputHeight, 1 To 1) 
... 
strippedArray(i, 1) = currentInputAsInt 
+0

謝謝Mr. Creamy! – nightTrevors

相關問題