我在下面开发的功能代码工作正常,我使用的公式是=adj(cell1,cell2)
。
现在我想让它成为任何j
值的动态函数,以便我可以将它用于工作表中的任何数据(某些数据有j value = 500
)。
Public Function adj(x As String, y As String) As String
Dim i, j As Integer
Dim t, z As String
Sheet1.Select
With Sheet1
j = x
z = Chr(10)
i = 1
If j = 3 Then
t = Evaluate(y & (i) & "))") & z & _
Evaluate(y & (i + 1) & "))") & z & _
Evaluate(y & (i + 2) & "))")
ElseIf j = 2 Then
t = Evaluate(y & (i) & "))") & z & _
Evaluate(y & (i + 1) & "))")
ElseIf j = 1 Then
t = Evaluate(y & (i) & "))")
End If
adj = t
End With
End Function
答案 0 :(得分:2)
正确的方向,但有几点我想指出:
代码:
Public Function adj(x As String, y As String) As String
Dim j As Long
Dim t As String
t = ""
j = x
For k = 1 To j
If k<j Then
t = t & Evaluate(y & (k) & "))") & Chr(10)
Else
t = t & Evaluate(y & (k) & "))")
End If
Next k
adj = t
End Function