函数在数组计算后不写入所需的单元格; excel vba 用户表单
Function not writing to required cell following array calculation ; excel vba userforms
我是 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.
我的问题是函数没有写入 'Result' 单元格。我做错了什么吗?我的问题是 excel 数组如何读取“”字符串?
感谢任何帮助。
您的函数从不写入 result 单元格,因为您实际上从未告诉它写入单元格。
这一行 Result = ws.Cells(Rngrow, 6).Value
仅将变量 Result 设置为代码为 运行 时特定单元格中存在的值。
然后您的 If
块将重置 Result
变量。
看下面的代码,我觉得比较符合你的要求。我添加了代码以遍历数组并检查 1
,然后根据数组中的内容设置结果单元格中的值。我还添加了更好的变量限定来匹配类型。我还把它设为 Sub
,因为它没有 return 任何值,就像 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) 来测试整个案例。您必须根据您的标准测试数组中的每个元素。
我是 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.
我的问题是函数没有写入 'Result' 单元格。我做错了什么吗?我的问题是 excel 数组如何读取“”字符串?
感谢任何帮助。
您的函数从不写入 result 单元格,因为您实际上从未告诉它写入单元格。
这一行 Result = ws.Cells(Rngrow, 6).Value
仅将变量 Result 设置为代码为 运行 时特定单元格中存在的值。
然后您的 If
块将重置 Result
变量。
看下面的代码,我觉得比较符合你的要求。我添加了代码以遍历数组并检查 1
,然后根据数组中的内容设置结果单元格中的值。我还添加了更好的变量限定来匹配类型。我还把它设为 Sub
,因为它没有 return 任何值,就像 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) 来测试整个案例。您必须根据您的标准测试数组中的每个元素。