gpt4 book ai didi

vba - 调用 sub 不起作用,但代码在放置在执行调用的 sub 中时有效

转载 作者:行者123 更新时间:2023-12-04 21:22:11 32 4
gpt4 key购买 nike

对你们来说应该是一个快速和简单的,为什么这不起作用?

代码限制用户只能在文本框中输入文本。
它工作正常,但我有大约 50 个文本框,所以会更干净,更容易调用。

但是,这样做,限制不再起作用

Private Sub OnlyAcceptText()
'Forces the textbox to only accept text
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122)Then KeyAscii = 0
End Sub

KeyPress在文本框中的代码:
Private Sub AgencyContactTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Calls sub that only allows text in texctbox
Call OnlyAcceptText
End Sub

最佳答案

使用事件下沉,你有一个类,里面有一个事件下沉文本框,像这样

Private WithEvents t As MSForms.TextBox

Public Sub INITIALISE(tb As MSForms.TextBox)
Set t = tb
End Sub

Private Sub t_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122) Then KeyAscii =0
End Sub

然后你需要创建一个集合来保存 50 个类,这将模仿 me.controls作为一个集合,但仅适用于自定义文本框。
Public colCustomTextboxes As New Collection

Private Sub UserForm_Initialize()
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
set t=new clsCustomTextBox
t.INITIALISE c
colCustomTextboxes.Add t
End If
Next c
End Sub

关于vba - 调用 sub 不起作用,但代码在放置在执行调用的 sub 中时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42760029/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com