使用宏(VBA代码)刷新Excel工作表中的所有引用字段

时间:2013-04-17 01:35:47

标签: excel vba spreadsheet

我有一个电子表格,可以从同一电子表格中的另一个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

或者我应该只接受一个Pivot表?

1 个答案:

答案 0 :(得分: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