我已经激活了"sheet2“,但即便如此,sum函数也会从"sheet1”而不是"sheet2“中获取值。
Worksheets("sheet2").Activate
Sheets("sheet2").Cells(fl1, nf) = Application.WorksheetFunction.Sum(Range(Cells(fl1, locrng1.Column), Cells(fl1, (nf - 1))))
Sheets("sheet2").Cells(fl1, of) = Sheets("Sheet2").Cells(fl1, nf) * Sheets("Sheet2").Cells(fl1, firstlevel1.Offset(0, 1).Column)
Sheets("sheet2").Cells(fl1, rf) = Sheets("Sheet2").Cells(fl1, nf) * (1 - (Sheets("Sheet2").Cells(fl1, firstlevel1.Offset(0, 1).Column)))发布于 2017-05-04 14:06:23
您应该了解如何使用With语句,它将为您节省大量的输入工作。
注意:在你的代码中,你在哪里使用Set firstlevel1?这可能是在您将Activate与Worksheets("sheet2").Activate一起使用之前。
请记住,几乎没有任何理由使用Activate,它只会减慢代码运行时的速度。
尝试下面的代码:
With Worksheets("sheet2")
.Cells(fl1, nf) = Application.WorksheetFunction.Sum(.Range(.Cells(fl1, locrng1.Column), .Cells(fl1, (nf - 1))))
.Cells(fl1, of) = .Cells(fl1, nf) * .Cells(fl1, firstlevel1.Offset(0, 1).Column)
.Cells(fl1, rf) = .Cells(fl1, nf) * (1 - (.Cells(fl1, firstlevel1.Offset(0, 1).Column)))
End Withhttps://stackoverflow.com/questions/43774807
复制相似问题