【发布时间】:2011-12-06 07:02:55
【问题描述】:
我在这里重新发明轮子吗?有一个更好的方法吗?此 VBA 函数在 Access 中的表单的注释字段中查找字符串的第一个实例,该字符串包含 20 个或更少字符,没有空格,用 (~) 波浪线括起来,然后返回它。
Public Function ParseComment(strComment As String) As String
' This function parses the comment field of the job entry dialog for (~) tilde
' surrounded text, then returns that text.
Dim intCounter As Integer
Dim intFirstChar As Integer
Dim intLastChar As Integer
Dim strResult As String
intFirstChar = 0
intLastChar = 0
intCounter = 0
Do While (intLastChar = 0) And (intCounter < Len(strComment))
intCounter = intCounter + 1
strCharacter = Mid(strComment, intCounter, 1)
If (strCharacter = "~") Then
If intFirstChar Then
intLastChar = intCounter
Else
intFirstChar = intCounter + 1
End If
End If
Loop
strResult = Mid(strComment, intFirstChar, intLastChar - intFirstChar)
If (intLastChar - intFirstChar <= 20) And (intFirstChar <> 0 Or intLastChar <> 0) And Not InStr(strResult, " ") Then
ParseComment = strResult
End If
End Function
非常感谢。
【问题讨论】:
-
总是可以更好地编写代码,但如果这对你有用,为什么还要麻烦?
-
只是一个旁注,你可以做
Dim intFirstChar as Integer=0它可以用你所有的初始化来清理代码,当你声明它们时初始化它们。 -
@JonH "Dim intFirstChar As Integer = 0" 将导致 VBA 中的编译错误。 VBA 已经将局部整数变量初始化为零,因此即使有可能也毫无意义。但是它可以在 VB.NET 中运行。
-
啊好吧不知道,只是注意到它是 vba,以为他只是在做 vb.net。谢谢
-
这个问题很危险地接近投票结束“不具建设性”或“不是一个真正的问题”,因为“喋喋不休的开放式问题会降低我们网站的实用性并将其他问题排除在外首页。” (常见问题解答)我认为您应该明确表达担忧并缩小问题范围。只是说。