用户窗体文本框的.PasswordChar属性在Mac Excel中不起作用

时间:2018-06-24 13:25:31

标签: excel-vba-mac

.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

1 个答案:

答案 0 :(得分:0)

这很难解决。 Userform文本框的.PasswordChar属性不适用于MAC Excel。因此,如果我想使其成为Excel Applications更可移植,更通用,那么我必须对其执行相同的结果进行编码。

无论如何,我通过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

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

相关问题