简体   繁体   English

复制范围内的VBA自动递增单元格

[英]Copy VBA Auto increment cell in range

I have an excel sheet with multiple columns, One column has reference(s) to previous (just like in MS-Project), 我有一个包含多列的excel工作表,其中一列引用了以前的引用(就像在MS-Project中一样),

I am trying to get the row numbers for each of the reference(s) using the below function, this works well. 我正在尝试使用以下功能获取每个引用的行号,这很好。

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

End Function 结束功能

The problem is that when I copy the same to the below cells, it just copies the same content, and unless I manually edit each row the data is not updated. 问题是,当我将相同的内容复制到以下单元格时,它只会复制相同的内容,除非手动编辑每一行,否则数据不会更新。

Is there an easier way to update the value automatically? 有没有更简单的方法来自动更新值?

ActiveCell reference should not be used inside a Function. ActiveCell引用不应在Function内部使用。 You can rather reference the input range but requiring it as a function input variable and then having your function return the processed output. 您可以引用输入范围,但需要将其作为函数输入变量,然后让函数返回处理后的输出。 For example: 例如:

Public Function Splitter(CellReference As Range) As String
    Dim multiRef() As String
    multiValue = CellReference.Value2
    .
    .
    .
    Splitter = tempValue

End Function

You can then in your cell call your formula as: 然后,您可以在单元格中将公式称为:

U5 =SPLITTER(T5)

And when you copy down it will vary the input cell reference relative to the target cell, ie U6 = SPLITTER(T6) 当您复制下来时,它将相对于目标单元格改变输入单元格的引用,即U6 = SPLITTER(T6)

As I noticed you have also a hardcoded range that you do a Match on, you can also have that range as an input if it also varies through your dataset. 正如我注意到的那样,您还有一个进行Match的硬编码范围,如果该范围也随数据集而变化,则也可以将该范围作为输入。

Hope this helps. 希望这可以帮助。 Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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