我尝试使用VBA将公式插入Excel工作表,但公式只能插入一个工作表
Sub RunSheets1()
Sheets("Sheet1").Activate
With ThisWorkSheet
Range("B5").Formula = "=SUM(F3,G3,H3,I3,J3)"
End With
Sheets("Sheet2").Activate
With ThisWorkSheet
Range("H6").Formula = "=SUM(F4,G4,H4,I4,J4)"
Range("AC6").Formula = "=SUM(AC4,AD4,AE4,AF4,AG4)"
End With
Sheets("Sheet3").Activate
With ThisWorkSheet
Range("H6").Formula = "=SUM(F4,G4,H4,I4,J4)"
Range("AC6").Formula = "=SUM(AC4,AD4,AE4,AF4,AG4)"
End With
Sheets("Sheet4").Activate
With ThisWorkSheet
Range("H6").Formula = "=SUM(F4,G4,H4,I4,J4)"
Range("AC6").Formula = "=SUM(AC4,AD4,AE4,AF4,AG4)"
End With
Sheets("Sheet5").Activate
With ThisWorkSheet
Range("H6").Formula = "=SUM(F4,G4,H4,I4,J4)"
Range("AC6").Formula = "=SUM(AC4,AD4,AE4,AF4,AG4)"
End With
End Sub
2条答案
按热度按时间uyto3xhc1#
要在工作表中填充,可以使用类似以下的代码:
tzdcorbm2#
您应该使用工作表和范围的显式引用:
Range("B5")
总是隐式引用 * 活动工作表 *。您应该明确避免这种情况!此外,我建议阅读How to avoid using select。