【发布时间】: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。