我希望能够做到以下几点:
D E F
1 P Q pf
2 100 =D2/F2*SIN(ACOS(F2)) =D2/SQRT((D2^2+E2^2))
3 200 =D3/F3*SIN(ACOS(F3)) =D3/SQRT((D3^2+E3^2))
4
5 P Q pf
6 100 =D6/F6*SIN(ACOS(F6)) =D6/SQRT((D7^2+E6^2))
7 200 =D7/F7*SIN(ACOS(F7)) =D7/SQRT((D7^2+E7^2))
也就是说,如果我在列F中输入值,则应自动填写E列中的值,如果我在E列中输入值,则应自动填写F列中的值。
但是,在输入值之前,我不想显示#DIV/0!
。
目标:
此外,我将在某些行中输入名称,因此必须忽略这些行。我知道我可以将方程式向下拖动并以此方式实现,但是再次,这将给我#DIV/0!
。
有一种聪明的方法吗?我正在使用Excel 2010。
答案 0 :(得分:1)
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中输入数字,应计算其他公式,结果应显示在相邻单元格中。