简体   繁体   中英

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

I am new to vba and need some help please. 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 "".

    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. If one element of the array is occupied by a "1" - the result is Rej. Only if all the elements of the array 'Arr' contain "" do I want the result to be 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?

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.

Your If block then just resets the Result variable.

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. 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 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. You have to test each element in the array against your criteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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