我有一个包含多列的Excel工作表,一列有前一个参考(就像在MS-Project中一样),
我正在尝试使用以下函数获取每个引用的行号,这很有效。
Public Function Splitter() As String
Dim multiRef() As String
Dim ws As Worksheet, rowNumber As Integer
Dim multiValue As String
Dim rowIndex As String, tempValue As String
Set ws = Worksheets("Sheet1")
rowNumber = ActiveCell.row
multiValue = ws.Range("T" & rowNumber).Value
multiRef = Split(multiValue, ";")
For i = 0 To UBound(multiRef)
If ((StrComp(Trim(multiRef(i)), "-") = 0) Or (StrComp(Trim(multiRef(i)), "Applicable") = 0) Or (StrComp(Trim(multiRef(i)), "") = 0) Or (StrComp(Trim(multiRef(i)), "TBD") = 0) Or (StrComp(Trim(multiRef(i)), "Y") = 0)) Then
Else:
tempValue = Application.WorksheetFunction.Match(Trim(multiRef(i)), ws.Range("E2:E1500"), 0)
If (Not IsNull(tempValue)) Then
rowIndex = rowIndex & tempValue & ", "
End If
End If
Next i
If (Len(rowIndex) <= 0) Then
Splitter_System = ""
Else: Splitter_System = Left(rowIndex, Len(rowIndex) - 2)
End If
结束功能
问题在于,当我将其复制到下面的单元格时,它只复制相同的内容,除非我手动编辑每一行,否则数据不会更新。
是否有更简单的方法自动更新值?
答案 0 :(得分:1)
不应在Function中使用ActiveCell引用。您可以引用输入范围,但需要将其作为函数输入变量,然后让函数返回已处理的输出。例如:
Public Function Splitter(CellReference As Range) As String
Dim multiRef() As String
multiValue = CellReference.Value2
.
.
.
Splitter = tempValue
End Function
然后,您可以在您的单元格中将您的公式称为:
U5 =SPLITTER(T5)
当你向下复制时,它将改变相对于目标细胞的输入细胞参考,即U6 = SPLITTER(T6)
正如我注意到您还有一个硬编码范围,您可以使用Match
,如果它也因数据集而异,您也可以将该范围作为输入。
希望这会有所帮助。欢呼声,