使用Excel-VBA for Mac添加两个单元格时出错

我正在尝试在Excel VBA for Mac中编写我的第一个代码。 我想要做的就是添加两个数字。 听起来很难,对吗? 我不断收到这个错误:

编译错误:无效的财产使用。

然后debugging器窗口打开,第一行突出显示。

Sub abbas() 'adds two numbers 'from e2 and e3 and puts the result in e4 Sheets("SHEET 1").Select Range("e2").Select A = ActiveCell.Value Range("e3").Select B = ActiveCell.Value Range("e4").Select ActiveCell.Value = A + B Range ("e5") End Sub 

错误

错误是由行提出的
Range ("e5")
你没有把这个范围的行为联系起来。 你想select它? 你想清除它? 编译器不理解。

改变代码的命题

我会给你一个2个简单的代码,没有任何错误

代码1

Sub abbas() 'adds two numbers 'from e2 and e3 and puts the result in e4 With Sheets("SHEET 1") A = .Range("e2") B = .Range("e3") .Range("e4") = A + B End With End Sub

代码2

  Sub abbas2() 'adds two numbers 'from e2 and e3 and puts the result in e4 With Sheets("SHEET 1") .Range("e4") = .Range("e2") + .Range("e3") End With End Sub 

笔记

尽量避免SelectActivate ,你不需要它们,你可以直接参考我的示例代码中显示的工作表上的值。
在sub / function的顶部声明你的variables是一个好习惯。
考虑使这个子function。 两者之间的区别