vba 删除字符串的第一个字符,如果它等于某物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8633223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing the first character of a string if it equals something
提问by Asynchronous
I am practicing VBA for Access 2010.
我正在为 Access 2010 练习 VBA。
I read all the suggested post regarding my post but did not find anything specific. I know how to move specific characters in a string, what I do not know is how I can remove specific character that equals to something.
我阅读了有关我的帖子的所有建议帖子,但没有找到任何具体内容。我知道如何移动字符串中的特定字符,但我不知道如何删除等于某物的特定字符。
I want to move the character 1 or 1- from telephone numbers if one is there.
如果有电话号码,我想从电话号码中移动字符 1 或 1- 。
Example: 17188888888 to 7188888888 or 1-7188888888 to 7188888888
示例:17188888888 至 7188888888 或 1-7188888888 至 7188888888
I am trying to use an if statement starting first with just removing the 1.
我试图首先使用 if 语句,只需删除 1。
The phone number is entered as a string not numbers.
电话号码以字符串而非数字形式输入。
This is what I have started: I am getting an error message that RemoveFirstCharis ambiguous.
这就是我的开始:我收到一条错误消息,指出RemoveFirstChar不明确。
Public Function RemoveFirstChar(RemFstChar As String) As String
If Left(RemFstChar, 1) = "1" Then
RemFstChar = Replace(RemFstChar, "1", "")
End If
RemoveFirstChar = RemFstChar
End Function
回答by Karel
I have tested your function in Access 2010 and it worked just fune.. You can also use this code:
我已经在 Access 2010 中测试了您的功能,它运行起来很有趣。您也可以使用以下代码:
Public Function RemoveFirstChar(RemFstChar As String) As String
Dim TempString As String
TempString = RemFstChar
If Left(RemFstChar, 1) = "1" Then
If Len(RemFstChar) > 1 Then
TempString = Right(RemFstChar, Len(RemFstChar) - 1)
End If
End If
RemoveFirstChar = TempString
End Function
回答by Fionnuala
There is nothing particularly wrong with your code, the "ambiguous" error message in this context is very likely to be because you have another sub or function in a different module with the same name. Search to find the duplicate name.
您的代码没有什么特别的错误,在此上下文中的“模棱两可”错误消息很可能是因为您在不同的模块中有另一个具有相同名称的子或函数。搜索以找到重复的名称。
If you put the function in the module belonging to a form or report, it is probably best to skip "Public". If you intend the function to be used by several forms, create a new module that is not attached to a form and put the functions that are intended for all forms and reports in that.
如果将该函数放在属于表单或报表的模块中,则最好跳过“公共”。如果您希望该功能由多个表单使用,请创建一个不附加到表单的新模块,并将适用于所有表单和报表的功能放入该模块中。
It is nearly always good to provide the full error message and error number.
提供完整的错误消息和错误编号几乎总是好的。