0

我有一个电子表格,它从同一个电子表格上的另一个 Excel 选项卡中引用其大部分值=Data!D11,例如,使用它所引用的值更新字段值。

我之前编写了以下 VB MACRO 代码,它提供了一个弹出框来输入要更新的行号,其中包含所选行的硬编码字段,但是如果我想更新所有字段,例如能够选择哪些列和需要更新的行?,我基本上需要一个宏 VB 代码,如果用户请求,我可以更新页面上引用的所有字段。

我对为什么 Excel 自己不更新它们有点困惑,但是这种自动化应该有助于通过运行宏来更新电子表格中的所有引用字段,除非有另一种方法可以实际“刷新”字段以显示新值?

Sub UpdateFormulas()

    Dim LRowNumber As Long

    LRowNumber = InputBox("Please enter the row number to update the formulas.")

    Sheets("Form").Select

    'All following code will copy a formula into the destination if the source
    'has a value.  If the source does not have a value, it will copy a blank to
    'the destination.

    'Item #1
    Range("F7").Select
    If IsEmpty(Range("Data!A" & LRowNumber).Value) Then
        ActiveCell.Value = ""
    Else
        ActiveCell.Formula = "=Data!A" & LRowNumber
    End If

    'Item #2
    Range("F9").Select
    If IsEmpty(Range("Data!B" & LRowNumber).Value) Then
        ActiveCell.Value = ""
    Else
        ActiveCell.Formula = "=Data!B" & LRowNumber
    End If

    'Item #3
    Range("J10").Select
    If IsEmpty(Range("Data!C" & LRowNumber).Value) Then
        ActiveCell.Value = ""
    Else
        ActiveCell.Formula = "=Data!C" & LRowNumber
    End If

    'Item #4
    Range("H11").Select
    If IsEmpty(Range("Data!D" & LRowNumber).Value) Then
        ActiveCell.Value = ""
    Else
        ActiveCell.Formula = "=Data!D" & LRowNumber
    End If

    'Item #5
    Range("D11").Select
    If IsEmpty(Range("Data!E" & LRowNumber).Value) Then
        ActiveCell.Value = ""
    Else
        ActiveCell.Formula = "=Data!E" & LRowNumber
    End If

    'Reposition back on item #1
    Range("F7").Select

    MsgBox ("The formulas were successfully updated to row " & LRowNumber & ".")

End Sub

或者我应该只接受一个数据透视表吗?

4

1 回答 1

1

只需循环执行即可。

Sub UpdateFormulas()

Dim LRowNumber As Long
Dim FRowNumber as Long
Dim r as Long

FRowNumber = InputBox("Please enter the first row number to update the formulas.")
LRowNumber = InputBox("Please enter the last row number to update the formulas.")

Sheets("Form").Select

For r = FRowNumber to LRowNumber

         'Item #1
        With Range("F7")
        If IsEmpty(Range("Data!A" & r).Value) Then
           .Value = ""
        Else
            .Formula = "=Data!A" & r
        End If
        End With

        'Item #2
        With Range("F9")
        If IsEmpty(Range("Data!B" & r).Value) Then
            .Value = ""
        Else
            .Formula = "=Data!B" & r
        End If
        End With

        'Item #3
        With Range("J10")
        If IsEmpty(Range("Data!C" & r).Value) Then
            .Value = ""
        Else
            .Formula = "=Data!C" & r
        End If
        End With

        'Item #4
        With Range("H11")
        If IsEmpty(Range("Data!D" & r).Value) Then
            .Value = ""
        Else
            .Formula = "=Data!D" & r
        End If
        End With

        'Item #5
        With Range("D11")
        If IsEmpty(Range("Data!E" & r).Value) Then
            .Value = ""
        Else
            .Formula = "=Data!E" & r
        End If
        End With

Next

    MsgBox ("The formulas were successfully updated to rows " & _
        FRowNumber & " to " & LRowNumber & ".")


End Sub

如果不想依赖输入框,可以动态设置FRowNumberLRowNumber变量。就像是:

FRowNumber = ActiveSheet.UsedRange.Rows(1).Row
LRowNumber = ActiveSheet.UsedRange.Rows.Count
于 2013-04-17T01:53:21.733 回答