【问题标题】:Function not writing to required cell following array calculation ; excel vba userforms函数未写入数组计算后所需的单元格; excel vba 用户表单
【发布时间】:2016-01-25 15:51:06
【问题描述】:

我是 vba 新手,需要一些帮助。 我有一个函数,其作用是根据特定条件向单元格写入文本值。

'函数的输入将是从用户表单中的组合框获得的字符串。 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

由于数组 'Arr' 将只有“1's”或“”的值,因此条件测试数组中是否有任何内容。如果数组的一个元素被“1”占据 - 结果是 Rej。只有当数组 'Arr' 的所有元素都包含 "" 时,我才希望结果为 Vir。

我的问题是函数没有写入“结果”单元格。有什么我做错了吗?我对 excel 数组如何读取“”字符串有问题吗?

任何帮助表示赞赏。

【问题讨论】:

  • 函数结束前添加State = Result。

标签: arrays vba excel userform


【解决方案1】:

您的函数永远不会写入 result 单元格,因为您实际上从未告诉它写入单元格。

Result = ws.Cells(Rngrow, 6).Value 这一行仅将变量 Result 设置为代码运行时特定单元格中存在的值。

您的 If 块然后只需重置 Result 变量。

看下面的代码,我觉得更符合你的要求。我添加了代码来循环遍历数组并检查1,然后根据数组中的内容设置结果单元格中的值。我还添加了更好的变量限定来匹配类型。我也将其设为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

还有一点需要注意,我遍历了数组,因为每个数组都有一个元素。即使那个元素 = ""。因此,您不能只使用 Len(Arr) 来测试整个案例。您必须根据您的标准测试数组中的每个元素。

【讨论】:

  • 感谢@Scott Holtzman。你的逻辑是有道理的。但是,我遇到了运行时错误 '424': Object required with this line Set Rng = ws.Columns(5).Find(DieID).Address
  • 您似乎在Find(DieID) 中意外添加了字母Die 这不在您的OP 或我的答案中的代码中。并去掉行中的.Address。我将为此编辑我的答案。
  • 谢谢斯科特。关于“死”添加的道歉 - 我正在同时处理两段代码。 Set Rng = ws.Columns(5).Find(ID).Address 是正确的行。 .Address 是必需的,因为 Rngrow 变量从地址中提取行号。以前一直有这个问题,不知道为什么??
  • 保留 .Address,因为我将 rng 设置为 Range 变量,而 .Address 是字符串属性。我更改了行以使行也反映这一点。 ..Rngrow = rng.Row
  • (松了一口气)- 谢谢斯科特。像一个绝对的魅力一样工作!图例。
猜你喜欢
  • 2015-09-17
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2013-02-26
相关资源
最近更新 更多