将Sheet2中的动态数据拉入Sheet1,然后拉到Sheet3..10

时间:2010-10-26 04:51:06

标签: excel vba excel-vba

所以我有Sheet1Sheet2Sheet1是用户设置,Sheet2是由Sheet1中的不同值生成的值表。现在可以创建Sheet3..10,这将根据Sheet2的计算从Sheet3!H20中提取值。

我遇到的问题是从Sheet2!I15更新说Sheet3!H20时,我如何让=VALUE拥有=Sheet2!I15而不是Sheet4..10那么Workbook_SheetCalculate(ByVal Sh As Object)可以重用这个表吗?

我试过了Private Sub Workbook_SheetCalculate(ByVal Sh As Object) Select Case Sh.Name Case "Sheet1" Case "Sheet2" Case Else ThisWorkbook.Worksheets("Sheet2").Range("$C$14").Value = "=" & Sh.Range("$D$23").Value Sh.Range("$H$20").Value = "=" & ThisWorkbook.Worksheets("Sheet2").Range("$I$15").Value End Select End Sub

Worksheet_Calculate

但是,当我这样做时,它只是不断重新计算,最终Excel崩溃了。我还尝试使用{{1}}进入每个工作表,当我这样做时,我得到Range的运行时错误不在Worksheet对象中。

和想法?

2 个答案:

答案 0 :(得分:1)

尝试将此代码放在宏的开头......

Application.Calculation = xlManual

然后在结束时

Application.Calculation = xlAutomatic

为了确保在不调用无限循环的情况下执行所有计算,请尝试这样的操作。

1.Declare一个名为IsCalculating的全局变量
2.将代码包含在if语句中

if not IsCalculating then
    IsCalculating = true
    Application.Calculation = xlManual

    `put your code here`

    Application.Calculation = xlAutomatic
    Application.Calculate
    IsCalculating = false
end if

答案 1 :(得分:0)

我会尝试IsCalculating变量。为了让它在夜晚工作,我想出了这个......

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Select Case Sh.Name
        Case "Settings"
        Case "Year To Date"
        Case "Federal Table"
            Select Case ActiveSheet.Name
                Case "Settings"
                Case "Year To Date"
                Case "Federal Table"
                Case "State Table"
                Case "CO Table"
                Case "IA Table"
                Case Else
                    ActiveSheet.Range("$H$20") = "=" & Worksheets("Federal Table").Range("$I$15").Value
            End Select
        Case "State Table"
            Select Case ActiveSheet.Name
                Case "Settings"
                Case "Year To Date"
                Case "Federal Table"
                Case "State Table"
                Case "CO Table"
                Case "IA Table"
                Case Else
                    ActiveSheet.Range("$H$21") = "=" & Worksheets("State Table").Range("$H$6").Value
            End Select
        Case "CO Table"
        Case "IA Table"
        Case Else
            If Worksheets("Federal Table").Range("$C$14").Value <> Sh.Range("$D$23").Value Then
                Worksheets("Federal Table").Range("$C$14").Value = "=" & Sh.Range("$D$23").Value
                Worksheets("State Table").Range("$H$2").Value = "=" & Sh.Range("$D$23").Value
            End If
    End Select
End Sub

但它看起来很草率。