首页 > 解决方案 > 用户表单文本框的 .PasswordChar 属性在 Mac Excel 中不起作用

问题描述

.PasswordChar属性使用用户在 Excel for Windows 但不是 Excel for Mac 中正确选择的任何符号来掩盖文本框输入。

我通过Textbox1_Change事件工作,但它没有有效地工作。

谁能提示我该怎么做?

Private Sub TextBox1_Change()

    Dim mystring As Variant
    Dim textlen As Integer
    Dim counter As Integer

    mystring = UserForm1.TextBox1.Value
    textlen = VBA.Len(mystring)

    If VBA.Right(mystring, 1) = "*" Then
        Exit Sub
    ElseIf VBA.Right(mystring, 1) <> "*" Then
    End If

    If shes = vbNullString Then
    Else
    shes = Sheets("Sheet1").Range("A1").Value
    End If

    For counter = 1 To textlen
        If textlen > 0 Then
            If VBA.Mid(mystring, counter, 1) = "*" Then
            Else
                Sheets("Sheet1").Range("A1").Value = Sheets("Sheet1").Range("A1").Value & VBA.Mid(mystring, counter, 1)
            End If
        End If
    Next

    If textlen > 0 Then
        UserForm1.TextBox1.Value = VBA.Replace(mystring, VBA.Mid(mystring, textlen, 1), "*")
    End If

End Sub

这是在 Windows 和 MAC excel 中对我有用的代码。

但限制是代码不允许用户在编写时编辑文本框值。所以它以某种方式变得更加安全。这是代码。

    Private Sub TextBox1_Change()

    Dim mystring As Variant
    Dim textlen As Integer
    Dim counter As Integer
    Dim passlen As Integer

    mystring = UserForm1.TextBox1.Value
    textlen = VBA.Len(mystring)
    passlen = VBA.Len(Sheets("Sheet1").Range("A1").Value)

    If VBA.Right(mystring, 1) = "*" Then
        If passlen <> textlen Then
            MsgBox "You're not allowed to do so"
            UserForm1.TextBox1.Value = ""
            Sheets("Sheet1").Range("A1").Value = ""
            Exit Sub
        End If
    Exit Sub
    End If

    For counter = 1 To textlen
        If textlen > 0 Then
            If VBA.Mid(mystring, counter, 1) = "*" Then
            Else
                Sheets("Sheet1").Range("A1").Value = Sheets("Sheet1").Range("A1").Value & VBA.Mid(mystring, counter, 1)
            End If
        End If
    Next

    If textlen > 0 Then
        UserForm1.TextBox1.Value = VBA.Replace(mystring, VBA.Mid(mystring, textlen, 1), "*")
    End If

End Sub

标签: excelvbamacos

解决方案


这很难解决。用户表单文本框的 .PasswordChar 属性不适用于 MAC Excel。所以,如果我想让 Excel 应用程序更便携和通用,那么我必须编写代码来执行相同的结果。

无论如何,我通过 Textbox1_Change 事件找到了解决方案。我的版本的限制是只有当用户想要编辑或删除密码字符串的任何单个文本时,它才不允许用户这样做。我不认为这是一个限制,它可以为密码屏蔽提供更多的力量。

那么这里是代码:

Private Sub TextBox1_Change()

    Dim mystring As Variant
    Dim textlen As Integer
    Dim counter As Integer
    Dim passlen As Integer

    mystring = UserForm1.TextBox1.Value
    textlen = VBA.Len(mystring)
    passlen = VBA.Len(Sheets("Sheet1").Range("A1").Value)

    If VBA.Right(mystring, 1) = "*" Then
        If passlen <> textlen Then
            MsgBox "You're not allowed to do so"
            UserForm1.TextBox1.Value = ""
            Sheets("Sheet1").Range("A1").Value = ""
            Exit Sub
        End If
    Exit Sub
    End If

    For counter = 1 To textlen
        If textlen > 0 Then
            If VBA.Mid(mystring, counter, 1) = "*" Then
            Else
                Sheets("Sheet1").Range("A1").Value = Sheets("Sheet1").Range("A1").Value & VBA.Mid(mystring, counter, 1)
            End If
        End If
    Next

    If textlen > 0 Then
        UserForm1.TextBox1.Value = VBA.Replace(mystring, VBA.Mid(mystring, textlen, 1), "*")
    End If

End Sub

我正在使用 Cell A1 来存储实际的字符串,并将其用于登录设置。


推荐阅读