- 打开 VBE(Visual Basic 编辑器)ALT+F11
- 在项目资源管理器中右键单击并插入
Module
- 粘贴以下代码
UDF 函数
Function E()
Dim D As Range, F As Range
Set D = ActiveCell.Offset(0, -1): Set F = ActiveCell.Offset(0, 1)
If Len(D) = 0 Or Len(F) = 0 Then E = ""
If Len(F) > 0 Then
Dim result As Double
result = Evaluate("=" & D & "/" & F & "*SIN(ACOS(" & F & "))")
E = result
End If
End Function
Function F()
Dim D As Range, E As Range
Set D = ActiveCell.Offset(0, -2): Set E = ActiveCell.Offset(0, -1)
If Len(D) = 0 Or Len(E) = 0 Then F = ""
If Len(E) > 0 Then
Dim result As Double
result = Evaluate("=" & D & "/SQRT((" & D & "^2+" & E & "^2))")
F = result
End If
End Function
- 返回电子表格并输入新公式
- 单元格
E2
现在=E()
和单元格F2
现在=F()
将公式向下拖动
结束了
更新:
自动插入和计算
在Project Explorer
» 右键单击Sheet1
»View Code
插入以下代码
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 Then
FillF Target
ElseIf Target.Column = 6 Then
FillE Target
End If
End Sub
在Module1
用下面的代码替换你的代码
Option Explicit
Sub FillF(ByVal Target As Range)
Dim D As Range, E As Range, F As Range
Set E = Target
Set D = E.Offset(0, -1)
Set F = E.Offset(0, 1)
If Not IsError(E) Then
If Len(E) = 0 Then
Exit Sub
ElseIf Len(D) = 0 Then
Exit Sub
ElseIf Len(D) > 0 And Len(F) = 0 Then
F.Formula = "=" & D & "/SQRT((" & D & "^2+" & E & "^2))"
Exit Sub
Else
Exit Sub
End If
Else
F.ClearContents
End If
End Sub
Sub FillE(ByVal Target As Range)
Dim D As Range, E As Range, F As Range
Set F = Target
Set E = F.Offset(0, -1)
Set D = F.Offset(0, -2)
If Not IsError(F) Then
If Len(F) = 0 Then
Exit Sub
ElseIf Len(D) = 0 Then
Exit Sub
ElseIf Len(D) > 0 And Len(E) = 0 Then
E.Formula = "=" & D & "/" & F & "*SIN(ACOS(" & F & "))"
Exit Sub
Else
Exit Sub
End If
Else
E.ClearContents
End If
End Sub
现在,转到您的电子表格并在 E 列或 F 列中输入一个数字,另一个公式应该被计算并且结果应该出现在相邻的单元格中。