简体   繁体   English

VBA UDF变量/整数和变量/字符串数组仅将第一个值打印到输出单元格

[英]VBA UDF Variant/Integer and Variant/String Arrays are printing only the first value to output cells

The following works great (thanks to gracious assistance by this community!) 以下作品很棒(感谢这个社区的慷慨帮助!)

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

This function will copy an input range to an output perfectly. 此功能会将输入范围完美复制到输出。 However, when I do some operations on the inputArray, the arrays look perfect but in Excel, only the first value of the array prints to all the cells. 但是,当我对inputArray进行一些操作时,数组看起来很完美,但是在Excel中,只有数组的第一个值会打印到所有单元格。 In this example, I'm parsing out a number from some input strings. 在此示例中,我从一些输入字符串中解析出一个数字。

Input Range: 输入范围

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

Code: 码:

    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

Expected output: 预期产量:

1
10
100

Actual output: 实际输出:

1
1
1

Running through with the debugger, strippedArray contains the Variant/Integer values 1,10,100 at the locations strippedArray(1)/(2)/(3) respectively. 通过调试器运行,strippedArray分别在strippedArray(1)/(2)/(3)位置包含Variant / Integer值1,10,100。 The issue is that the range that I array enter in Excel only contains strippedArray(1) as far as I can tell. 问题是,据我所知,我数组在Excel中输入的范围仅包含strippedArray(1)。

Thank you! 谢谢!

Your strippedArray array, needs to be two dimensional if you are outputting back into an Excel worksheet / range (I've made the assumption you are running this as an array formula). 如果要输出回Excel工作表/范围,则strippedArray数组需要为二维(我假设您将其作为数组公式运行)。 Make the following changes: 进行以下更改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM