Excel Macro强制大写并单击按钮删除特殊字符

时间:2019-05-06 19:22:10

标签: excel vba

我有2个代码,但只有一个在VBA中工作。我有

Private Sub FINALIZEBTN_Click()

Dim response As VbMsgBoxResult
response = MsgBox("HAVE YOU COMPLETED THE FORM IN FULL?", vbYesNo)
If response = vbYes Then
    MsgBox "DO NOT FORGET TO SAVE AND SUBMIT THIS FORM"
    Else
If response = vbNo Then
    MsgBox "PLEASE REVIEW AND COMPLETE THE FORM IN FULL"
    Exit Sub
End If
End If

Dim cell As Range
    For Each cell In Range("$A$1:" & Range("$A$1").SpecialCells(xlLastCell).Address)
        If Len(cell) > 0 Then cell = UCase(cell)
    Next cell

    Application.ScreenUpdating = True
End Sub

因此,在单击时会出现是/否提示,然后在整个工作表中强制使用大写字母。

我们允许的唯一符号是“&”和“-” 当输入特殊字符告诉他们“嘿,你不能做”之类的消息时,或者当发现特殊字符将其删除而又什么也没有删除时,我都希望弹出另一个框。如果我们能够删除并用尖锐字符替换拉丁字母(例如西班牙语),那也很好。当前,使用模块1中的代码保存或运行宏时,我看不到任何更改。

我在模块1中有以下代码

Function removeSpecial(sInput As String) As String
    Dim sSpecialChars As String
    Dim i As Long
    sSpecialChars = "\/:*?""<>|$,.`"
    For i = 1 To Len(sSpecialChars)
        sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
    Next
    removeSpecial = sInput
End Function

2 个答案:

答案 0 :(得分:0)

正如其他人所说,您需要致电removeSpecial

也就是说,我将重写removeSpecial来指定要保留的字符,因为特殊字符比removeSpecial中列出的特殊字符还要多

其他更改

  • 使用SpecialCells xlCellTypeConstants仅循环包含值的单元格(删除需要测试Len并排除公式)。
  • 可能性表没有常数值
  • 添加了带重音符号的替换:您将需要扩展ReplaceFromReplaceWith字符串以包括所需的所有替换(确保这两个字符串的长度相同)
  • 您可能(或可能不想)在包含的内容中包含其他字符,例如空格或其他标点符号?如果是这样,请将其添加到sKeepChars Like模式中(将-保留为第一个字符,然后将[]
  • 所有CAPS邮件都很丑陋!

Function removeSpecial(sInput As String) As String
    Dim sKeepChars As String
    Dim sClean As String
    Dim c As String
    Dim i As Long, j As Long
    Const ReplaceFrom As String = "AE"
    Const ReplaceWith As String = "ÀÊ"

    sKeepChars = "[-&A-Z" & ReplaceWith & "]"
    For i = 1 To Len(sInput)
        c = Mid$(sInput, i, 1)
        If c Like sKeepChars Then
            j = InStr(ReplaceFrom, c)
            If j Then
                c = Mid$(ReplaceWith, j, 1)
            End If
            sClean = sClean & c
        End If
    Next
    removeSpecial = sClean
End Function


Private Sub FINALIZEBTN_Click()
    Dim response As VbMsgBoxResult
    response = MsgBox("Have you completed the form in full?", vbYesNo)
    If response = vbYes Then
        MsgBox "Do not forget to save and submit this form"
    ElseIf response = vbNo Then
        MsgBox "Please review and complete the form in full"
        Exit Sub
    End If

    Application.ScreenUpdating = False
    Dim cell As Range
    Dim rng As Range
    With ActiveSheet
        On Error Resume Next
            Set rng = .Cells.SpecialCells(xlCellTypeConstants)
        On Error GoTo 0
        If Not rng Is Nothing Then
            For Each cell In rng
                cell = removeSpecial(UCase(cell))
            Next cell
        End If
    End With
    Application.ScreenUpdating = True
End Sub

答案 1 :(得分:-1)

这应该可以正常工作:

    Dim MyStr As String
    For Each cell In Range("$A$1:" & Range("$A$1").SpecialCells(xlLastCell).Address)
        If Len(cell) > 0 Then
            MyStr = cell
            cell = UCase(removeSpecial(MyStr))
        End If
    Next cell