简体   繁体   English

数组计算后函数未写入所需单元格; excel vba 用户表单

[英]Function not writing to required cell following array calculation ; excel vba userforms

I am new to vba and need some help please.我是 vba 新手,需要一些帮助。 I have a function whose role is to write to a cell a text value depending on a certain condition.我有一个函数,其作用是根据特定条件将文本值写入单元格。

'input to the function will be a string obtained from a combobox within a userform. '函数的输入将是从用户表单中的组合框获得的字符串。 The Rowarray is an array of 53 cells with values of either "1's" or "". Rowarray 是一个包含 53 个单元格的数组,其值为“1”或“”。

    Function State(ID As String, Rowarray() As String) 

    Dim ws As Worksheet
    Dim Rej As String
    Dim Vir As String
    Dim Result As Variant
    Dim Rng
    Dim Rngrow As Integer
    Dim Arr

    Set ws = ThisWorkbook.Worksheets("Temp")

    Set Rng = ws.Columns(5).Find(ID).Address 'obtains the address of a cell with a value matching the combobox value.
    Rngrow = ws.Range(Rng).row 'Obtains the row number from address above
    Rej = "R"
    Vir = "V"
    Arr = Rowarray()

    Result = ws.Cells(Rngrow, 6).Value 'this cell is where output is to be placed - one column adjacent to the ID cell (same row)


        'conditional statements determining what goes into result cell
        If Len(Arr) >= 1 Then

        Result = Rej

        ElseIf Len(Arr) < 1 Then

        Result = Vir

        Else

        Result = Vir

        End If

End Function

Since the array 'Arr' will only have values of "1's" or "", the condition tests whether the array will have anything in it at all.由于数组 'Arr' 的值只有 "1's" 或 "",所以条件测试数组中是否有任何内容。 If one element of the array is occupied by a "1" - the result is Rej.如果数组的一个元素被“1”占用 - 结果是 Rej。 Only if all the elements of the array 'Arr' contain "" do I want the result to be Vir.只有当数组 'Arr' 的所有元素都包含 "" 时,我才希望结果为 Vir。

My problem is that the function is not writing to the 'Result' cell.我的问题是该函数没有写入“结果”单元格。 Is there anything that I am doing wrong?有什么我做错了吗? Is my problem with how excel arrays read "" strings?我的问题是 excel 数组如何读取 "" 字符串吗?

Any help appreciated.任何帮助表示赞赏。

Your function never writes to the result cell because you actually never tell it to write to the cell.您的函数永远不会写入结果单元格,因为您实际上从未告诉它写入单元格。

This line Result = ws.Cells(Rngrow, 6).Value only sets the variable Result to the value existing in the specific cell at the time the code is run.这一行Result = ws.Cells(Rngrow, 6).Value仅将变量 Result 设置为代码运行时特定单元格中存在的值。

Your If block then just resets the Result variable.您的If块然后只是重置Result变量。

See the below code, which I think is more in line with what you want.请参阅下面的代码,我认为它更符合您的要求。 I added code to loop through the array and check for 1 , and then set the value in the result cell based on what's in the array.我添加了代码来循环遍历数组并检查1 ,然后根据数组中的内容设置结果单元格中的值。 I also added better variable qualifying to match types.我还添加了更好的变量限定来匹配类型。 I also made this a Sub since it does not return any value, like a Function would.我也把它变成了一个Sub因为它不返回任何值,就像一个Function一样。

Sub State(ID As String, Rowarray() As String)

    Dim ws As Worksheet
    Dim Rej As String, Vir As String
    Dim Rng As Range
    Dim Rngrow As Integer
    Dim Arr() As String

    Set ws = ThisWorkbook.Worksheets("Temp")

    Set Rng = ws.Columns(5).Find(ID) 'obtains the cell with a value matching the combobox value.

    If Not Rng Is Nothing Then

        Rngrow = Rng.Row 'Obtains the row number from range above

        Rej = "R"
        Vir = "V"

        Arr() = Rowarray()

        Dim l As Long, bRej As Boolean

        For l = LBound(Arr) To UBound(Arr)

            If Arr(l) = "1" Then
                ws.Cells(Rngrow, 6).Value = Rej
                bRej = True
                Exit For
            End If

        Next

        If Not bRej Then ws.Cells(Rngrow, 6).Value = Vir

    Else

        ws.Cells(Rngrow, 6).Value = "id not found"

    End If

End Sub

One more note, I looped through the array because each array has an element.还要注意的是,我遍历了数组,因为每个数组都有一个元素。 Even if that element = "" .即使那个元素 = "" So you cannot just use Len(Arr) to test the whole case.所以你不能只使用 Len(Arr) 来测试整个案例。 You have to test each element in the array against your criteria.您必须根据您的条件测试数组中的每个元素。

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

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