我想连接来自不同单元格的字符串并获得显示的结果,而无需一遍又一遍地输入连接公式,而只需使用 VBA/UDF 的一个函数,即可根据所选单元格获得不同的结果。
您真的确定您正在寻找用户定义的函数吗?
我的印象是您正在寻找绝对和相对参考,如下面的屏幕截图所示,基于此公式:
=C$35&C27&C$36&D27
如您所见,单元格“C35”和“C36”的行号前面有一个美元符号(变成“C$35”和“C$36”)。像这样,当我将公式拖到下一行时,这些行号不会改变,结果保持正确。
这是一个没有任何参数验证的演示片段。
Option Explicit
Function MyConcat(text1, text2) As String
Application.Volatile
MyConcat = Range("C35") & " " & _
text1 & " " & Range("C36") & " " & text2
End Function
M365 LAMBDA 公式
在M365中,您可以创建和调整以下lambda公式:
=LAMBDA(student,year,"The student named "&student&" is now on year "&year&".")
StudY
,然后在 Refers to
文本框中输入公式。=StudY(C31,D27)
即可。VBA
Function StudentYear(ByVal Student As String, ByVal Year As Long) As String
StudentYear = "The student named " & Student _
& " is now on year " & Year & "."
End Function
Function StudentYear2(ByVal Student As String, ByVal Year As Long) As String
Const BEFORE As String = "The student named "
Const BETWEEN As String = " is now on year "
Const AFTER As String = "."
StudentYear2 = BEFORE & Student & BETWEEN & Year & AFTER
End Function