Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 函数将字符串转换为大写_Vba_Excel_Excel Udf - Fatal编程技术网

Vba 函数将字符串转换为大写

Vba 函数将字符串转换为大写,vba,excel,excel-udf,Vba,Excel,Excel Udf,我一直在尝试使用VBA中的String.ToUpper()方法,让我编写的用户定义函数以大写形式返回其值。当我尝试在excel中使用我的自定义项时,我得到一个编译器错误,它只突出显示了我的自定义项的顶行: Function removeSpecial(sInput As String) As String 以下是完整的代码: Function removeSpecial(sInput As String) As String Dim sSpecialChars As String

我一直在尝试使用VBA中的
String.ToUpper()
方法,让我编写的用户定义函数以大写形式返回其值。当我尝试在excel中使用我的自定义项时,我得到一个编译器错误,它只突出显示了我的自定义项的顶行:

Function removeSpecial(sInput As String) As String
以下是完整的代码:

Function removeSpecial(sInput As String) As String
    Dim sSpecialChars As String
    Dim i As Long
    sSpecialChars = "\/:*?™""®<>|.&@# (_+`©~);-+=^$!,'" 'This is your list of characters to be removed
    For i = 1 To Len(sSpecialChars)
        sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")

    Next
    sInput = sInput.ToUpper()
    removeSpecial = sInput
End Function

如果此代码被注释掉,则我的UDF可以工作,但不会返回所有大写字母中输入的字符串。

只是函数错误。你想要

sInput = UCase(sInput)
希望这有助于确认UCase(…)功能是否正常工作。下面是另一个示例“将第二列中从第二行到结尾的第一个字母大写”:


对于VBA(不是VB.Net),只需使用
sInput=UCase(sInput)
就可以了。至少我没有疯。再次感谢simoco。答案已被接受,问题标题已更改。希望这能让用户在将来更容易找到。
sInput = UCase(sInput)
Sub UpCaseMacro()

' Declare variables
Dim OldValue As String
Dim NewValue As String
Dim FirstLetter As String
Dim i As Long

' Select values
lastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
ActiveSheet.Range(Cells(2, 2), Cells(lastRow, 2)).Select

' Update data
For i = 2 To Selection.Rows.Count
    If Not IsEmpty(Cells(i, 2).Value) Then
        OldValue = Cells(i, 2).Value
        FirstLetter = Left(Cells(i, 2).Value, 1)
        NewValue = UCase(FirstLetter) & Right(OldValue, Len(OldValue) - 1)
        Cells(i, 2).Value = NewValue
    End If
  Next i
 End Sub