Excel 自动选择每个工作表时出错

Excel 自动选择每个工作表时出错,excel,vba,Excel,Vba,我编辑了这个问题来发布整个代码。问题是只有工作簿中的活动工作表具有正确的计算。我已经用我的许多项目工作簿逐步完成了该程序,我发现列中的值 .Range("AT2").Value = Evaluate("SUMPRODUCT(AQ2:AQ152,AR2:AR152)/SUM(AQ2:AQ152)") 'calculating Fe weighted mean .Range("AT2").AutoFill Destination:=.Range("AT2:AT152") .Range("AU

我编辑了这个问题来发布整个代码。问题是只有工作簿中的活动工作表具有正确的计算。我已经用我的许多项目工作簿逐步完成了该程序,我发现列中的值

.Range("AT2").Value = Evaluate("SUMPRODUCT(AQ2:AQ152,AR2:AR152)/SUM(AQ2:AQ152)") 'calculating Fe weighted mean
  .Range("AT2").AutoFill Destination:=.Range("AT2:AT152")
  .Range("AU2").Value = Evaluate("2*SQRT(SUMPRODUCT((AR2:AR152-AT2)^2,$B$2:$B$152)/SUM($B$2:$B$152))/SQRT(COUNT(AQ2:AQ152))") 'calculating Fe weighted std dev
  .Range("AU2").AutoFill Destination:=.Range("AU2:AU152")

  .Range("AV2").Value = Evaluate("SUMPRODUCT(AQ2:AQ152,AS2:AS152)/SUM(AQ2:AQ152)") 'calculating Cr weighted mean
  .Range("AV2").AutoFill Destination:=.Range("AV2:AV152")
  .Range("AW2").Value = Evaluate("2*SQRT(SUMPRODUCT((AS2:AS152-AV2)^2,$B$2:$B$152)/SUM($B$2:$B$152))/SQRT(COUNT(AQ2:AQ152))") 'calculating Cr weighted std dev
  .Range("AW2").AutoFill Destination:=.Range("AW2:AW152")
…根据我在哪个活动工作簿中运行代码而变化。这4列:AT、AU、AV和AW完全从活动工作表复制到所有其他工作表。此错误传播到其他工作表的后一个代码行,给我错误的结果。只有活动工作表具有正确的结果。因此,这里的问题是,在活动工作表的4列中有数字的复制和粘贴,而不是计算所有其他工作表中使用的4个方程式及其各自的数据。我希望这是有道理的。请告诉我如何避免这个问题

谢谢

Sub main()
Dim titles() As Variant
Dim ws As Worksheet
Dim wb As Workbook
Dim i As Long
Dim j As Long


Application.ScreenUpdating = False

Set wb = ActiveWorkbook


titles() = Array("Distance (nm)", "Atom Count", "Fe %", "Cr %", "Fe (Weighted Mean)", "Fe (weighted Std error of mean)", "Cr (Weighted Mean)", "Cr(weighted Std error of mean)", "x", "Fe", "x", "Cr", "x", "Fe", "x", "Cr", "Fe Wave", "Fe Amp", "Cr Wave", "Cr Amp")

For Each ws In wb.Sheets

With ws

    .Range(.Cells(1, "AP"), .Cells(ActiveSheet.UsedRange.Rows.Count, "BI")).ClearContents

    For i = 41 + LBound(titles()) To 41 + UBound(titles())

        .Cells(1, 1 + i).Value = titles(i - 41)

    Next i
    .Rows(1).Font.Bold = True

End With



With ws

For i = 2 To 152
    .Cells(i, "AP").Value = .Cells(i, "A").Value 'Copy distance values from col A
Next i

    .Cells(2, "AQ").Value = ""
    .Cells(3, "AQ").Value = ""
For j = 4 To 152
    .Cells(j, "AQ").Value = (1 / 4) * (.Cells(j - 2, "C").Value + 2 * .Cells(j - 1, "C").Value + .Cells(j, "C").Value) 'doing 3 point weight avg for atom counts
Next j

    .Cells(2, "AR").Value = ""
    .Cells(3, "AR").Value = ""
For j = 4 To 152
    .Cells(j, "AR").Value = (1 / 4) * (.Cells(j - 2, "E").Value + 2 * .Cells(j - 1, "E").Value + .Cells(j, "E").Value) 'doing 3 point weight avg for Fe%
Next j

    .Cells(2, "AS").Value = ""
    .Cells(3, "AS").Value = ""
For j = 4 To 152
    .Cells(j, "AS").Value = (1 / 4) * (.Cells(j - 2, "K").Value + 2 * .Cells(j - 1, "K").Value + .Cells(j, "K").Value) 'doing 3 point weight avg for Cr%
Next j

  .Range("AT2").Value = Evaluate("SUMPRODUCT(AQ2:AQ152,AR2:AR152)/SUM(AQ2:AQ152)") 'calculating Fe weighted mean
  .Range("AT2").AutoFill Destination:=.Range("AT2:AT152")
  .Range("AU2").Value = Evaluate("2*SQRT(SUMPRODUCT((AR2:AR152-AT2)^2,$B$2:$B$152)/SUM($B$2:$B$152))/SQRT(COUNT(AQ2:AQ152))") 'calculating Fe weighted std dev
  .Range("AU2").AutoFill Destination:=.Range("AU2:AU152")

  .Range("AV2").Value = Evaluate("SUMPRODUCT(AQ2:AQ152,AS2:AS152)/SUM(AQ2:AQ152)") 'calculating Cr weighted mean
  .Range("AV2").AutoFill Destination:=.Range("AV2:AV152")
  .Range("AW2").Value = Evaluate("2*SQRT(SUMPRODUCT((AS2:AS152-AV2)^2,$B$2:$B$152)/SUM($B$2:$B$152))/SQRT(COUNT(AQ2:AQ152))") 'calculating Cr weighted std dev
  .Range("AW2").AutoFill Destination:=.Range("AW2:AW152")

End With


FindAllMaxima ws 'call on another sub to find the maxima

With ws

For j = 2 To .Cells(Rows.Count, "BB").End(xlUp).Row
If .Cells(j + 1, "BB").Value <> "" Then
.Cells(j, "BF").Value = .Cells(j + 1, "BB").Value - .Cells(j, "BB").Value 'calculate Fe Wavelength
End If
Next j

For j = 2 To .Cells(Rows.Count, "BD").End(xlUp).Row
If .Cells(j + 1, "BD").Value <> "" Then
.Cells(j, "BH").Value = .Cells(j + 1, "BD").Value - .Cells(j, "BD").Value 'calculate Cr Wavelength
End If
Next j

For j = 2 To .Cells(Rows.Count, "BB").End(xlUp).Row
If .Cells(j, "BB").Value <> "" Then
.Cells(j, "BG").Value = .Cells(j, "BC").Value - .Cells(j, "AT").Value 'calculate Fe Amplitude
End If
Next j

For j = 2 To .Cells(Rows.Count, "BD").End(xlUp).Row
If .Cells(j, "BD").Value <> "" Then
.Cells(j, "BI").Value = .Cells(j, "BE").Value - .Cells(j, "AV").Value 'calculate Cr Amplitude
End If
Next j

End With


Next ws


Application.ScreenUpdating = True

End Sub
Sub-main()
Dim titles()作为变体
将ws设置为工作表
将wb设置为工作簿
我想我会坚持多久
Dim j尽可能长
Application.ScreenUpdating=False
设置wb=ActiveWorkbook
titles()=数组(“距离(nm)”、“原子计数”、“铁%”、“铬%”、“铁(加权平均值)”、“铁(加权标准平均值误差)”、“铬(加权标准平均值误差)”、“x”、“铁”、“x”、“铁”、“x”、“铁”、“铁波”、“铁安培”、“铬波”、“铬安培”)
对于wb.Sheets中的每个ws
与ws
.Range(.Cells(1,“AP”),.Cells(ActiveSheet.UsedRange.Rows.Count,“BI”)).ClearContents
对于i=41+LBound(titles())到41+UBound(titles())
.单元格(1,1+i).值=标题(i-41)
接下来我
.Rows(1).Font.Bold=True
以
与ws
对于i=2到152
.Cells(i,“AP”).Value=.Cells(i,“A”).Value'从列A复制距离值
接下来我
.Cells(2,“AQ”).Value=“”
.Cells(3,“AQ”).Value=“”
对于j=4到152
.Cells(j,“AQ”)。Value=(1/4)*(.Cells(j-2,“C”)。Value+2*。Cells(j-1,“C”)。Value+。Cells(j,“C”)。Value)进行原子计数的平均3点加权
下一个j
.Cells(2,“AR”).Value=“”
.Cells(3,“AR”).Value=“”
对于j=4到152
.Cells(j,“AR”).值=(1/4)*(.Cells(j-2,“E”).值+2*.Cells(j-1,“E”).值+0.Cells(j,“E”).值)对Fe%进行3点加权平均
下一个j
.Cells(2,“AS”).Value=“”
.Cells(3,“AS”).Value=“”
对于j=4到152
.Cells(j,“AS”).值=(1/4)*(.Cells(j-2,“K”).值+2*.Cells(j-1,“K”).值+0.Cells(j,“K”).值)对Cr%进行3点加权平均
下一个j
.范围(“AT2”).值=计算Fe加权平均值的评估(“SUMPRODUCT(AQ2:AQ152,AR2:AR152)/SUM(AQ2:AQ152)”
.Range(“AT2”)。自动填充目标:=.Range(“AT2:AT152”)
.Range(“AU2”).Value=评估(“2*SQRT(SUMPRODUCT((AR2:AR152-AT2)^2,$B$2:$B$152)/总和($B$2:$B$152))/SQRT(计数(AQ2:AQ152))”)计算Fe加权标准偏差
.Range(“AU2”)。自动填充目标:=.Range(“AU2:AU152”)
.范围(“AV2”).值=计算Cr加权平均值的评估(“SUMPRODUCT(AQ2:AQ152,AS2:AS152)/SUM(AQ2:AQ152)”
.Range(“AV2”)。自动填充目标:=.Range(“AV2:AV152”)
.Range(“AW2”).Value=评估(“2*SQRT(SUMPRODUCT((AS2:AS152-AV2)^2,$B$2:$B$152)/总和($B$2:$B$152))/SQRT(计数(AQ2:AQ152))”)计算Cr加权标准偏差
.Range(“AW2”)。自动填充目标:=.Range(“AW2:AW152”)
以
FindAllMaxima ws呼叫另一艘潜艇寻找最大值
与ws
对于j=2到.单元格(Rows.Count,“BB”).End(xlUp).Row
如果.Cells(j+1,“BB”).值为“”,则
.Cells(j,“BF”)。值=.Cells(j+1,“BB”)。值-。Cells(j,“BB”)。值‘计算铁波长’
如果结束
下一个j
对于j=2到.Cells(Rows.Count,“BD”).End(xlUp).Row
如果.Cells(j+1,“BD”).Value“”则
.Cells(j,“BH”).值=.Cells(j+1,“BD”).值-.Cells(j,“BD”).值'计算Cr波长
如果结束
下一个j
对于j=2到.单元格(Rows.Count,“BB”).End(xlUp).Row
如果.Cells(j,“BB”).值为“”,则
.Cells(j,“BG”)。值=.Cells(j,“BC”)。值-。Cells(j,“AT”)。值‘计算铁振幅
如果结束
下一个j
对于j=2到.Cells(Rows.Count,“BD”).End(xlUp).Row
如果.Cells(j,“BD”).Value为“”,则
.Cells(j,“BI”).值=.Cells(j,“BE”).值-.Cells(j,“AV”).值'计算Cr振幅
如果结束
下一个j
以
下一个ws
Application.ScreenUpdating=True
端接头

谢谢,这是一个现已解决的问题。代码的其余部分还有其他问题,这些问题妨碍了除激活图纸外的所有图纸的正确计算。我将更深入地研究它,因为代码很长,并且调用了其他sub。我编辑了上面的问题,因为代码有更多的问题。你能帮我解决这个问题吗。谢谢我认为与其使用Evaluate,不如每次都将公式放在2,然后填写并粘贴值,如果你想覆盖公式。我不太理解你的评论。如果不进行计算,我将如何编写公式?你所说的“你最好每次都把公式放在2中……”是什么意思?我用一个简单的例子修改了上面的代码,希望你能适用于你的情况。一种选择仍然使用Evaluate。
Sub x()

Dim ws As Worksheet

For Each ws In Worksheets
    With ws
        .Range("A4") = Evaluate("SUM('" & ws.Name & "'!A1:A3)")
        'OR
        .Range("A4").Formula = "=SUM(A1:A3)"
        'Then autofill etc
    End With
Next ws

End Sub