您可以通過以下方式支持我:1,、關(guān)注、點(diǎn)贊,、留言,、分享、打賞,;2,、點(diǎn)擊感興趣的廣告、購買我的安利微店產(chǎn)品,;3,、添加我的合谷醫(yī)療企業(yè)微信,謝謝,! ☆快速瀏覽☆【2023年3月合集】【2023年4月合集】【2023年5月合集】【2023年6月合集】 ☆本期內(nèi)容概要☆ 完整代碼: 1,、Sheet1_CmdShow (命令按鈕) Private Sub CmdShow_Click() UserForm1.Show End Sub
代碼解析:
(1)啟動(dòng)用戶窗體UserForm1。2,、模塊1
(1)Pxy自定義函數(shù)
Function Pxy(arr() As Variant, searchValue As Variant) As Long t = LBound(arr) t = 1 - t For i = LBound(arr) To UBound(arr) If arr(i) = searchValue Then Pxy = i + t Exit Function End If Next Pxy = -1 ' 如果未找到值,,則返回 -1 End Function
代碼解析:用來查找定位查找值在數(shù)組中的位置
(A)t等于數(shù)組最大下標(biāo),,再使之等于1-t,,作用是修正數(shù)組最小下標(biāo)不為1時(shí)對(duì)應(yīng)的位置值。(B)通過i循環(huán)數(shù)組,,比對(duì)查找值與數(shù)組中的值,,若找到,則函數(shù)值等于t+1,。3,、類模塊(TextBoxEventHandler)
(1)定義變量 Public WithEvents txtBox As MSForms.TextBox Private previousValue As String
代碼解析:
(A)聲明一個(gè)公共變量 txtBox,,它是一個(gè)帶有事件處理程序的文本框?qū)ο蟆?/span>(B)聲明一個(gè)私有變量 previousValue,,用于存儲(chǔ)文本框在更新前的值。(2)ReceiveTextbox Public Sub ReceiveTextbox(ByVal reTextbox As MSForms.TextBox) Set txtBox = reTextbox End Sub
代碼解析:
(A)定義一個(gè)公共子過程 ReceiveTextbox,,用于接收一個(gè)文本框?qū)ο?,并將其賦值給 txtBox 變量。,。(B)將傳入的文本框?qū)ο筚x值給 txtBox 變量,。(3)txtBox_Change,文本框(TextBox)相關(guān)的事件處理程序 Private Sub txtBox_Change() On Error Resume Next Dim i As Integer i = CInt(Mid(txtBox.Name, 7)) With UserForm1 If .Controls("topLb_" & i).Caption = "金額" Then If Not IsNumeric(txtBox) Then MsgBox "請(qǐng)輸入數(shù)字,!" txtBox.Text = previousValue End If .Controls("topTb_" & i + 2).Text = CDbl(.Controls("topTb_" & i)) + CDbl(.Controls("topTb_" & i + 1)) ElseIf .Controls("topLb_" & i).Caption = "稅額" Then If Not IsNumeric(txtBox) Then MsgBox "請(qǐng)輸入數(shù)字,!" txtBox.Text = previousValue End If .Controls("topTb_" & i + 1).Text = CDbl(.Controls("topTb_" & i)) + CDbl(.Controls("topTb_" & i - 1)) ElseIf .Controls("topLb_" & i).Caption = "價(jià)稅合計(jì)" Then .Controls("topTb_" & i).Text = CDbl(.Controls("topTb_" & i - 2)) + CDbl(.Controls("topTb_" & i - 1)) End If End With End Sub
代碼解析:如果文本框發(fā)生了改變,激活其Change事件,。
(A)如果當(dāng)前文本框?qū)?yīng)的標(biāo)簽是金額,,限制僅輸入數(shù)字,同時(shí)價(jià)稅合計(jì)重新計(jì)算=金額+稅額,。如果當(dāng)前文本框?qū)?yīng)的標(biāo)簽是稅額,,操作同“金額“。(B)如果當(dāng)前文本框?qū)?yīng)的標(biāo)簽是價(jià)稅合計(jì),,則將其重新計(jì)算=金額+稅額,,直觀看來,不管輸入什么,,價(jià)稅合計(jì)總保持不變,,實(shí)現(xiàn)價(jià)稅合計(jì)不可編輯的目的。(4)txtbox_KeyDown Private Sub txtbox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) ' 在按下任意鍵之前,,存儲(chǔ)當(dāng)前值 previousValue = txtBox.Value End Sub
代碼解析:
(A)在按下任意鍵之前,,將當(dāng)前文本框的值存儲(chǔ)在 previousValue 變量中,供后續(xù)調(diào)用,。(B)這樣做的目的:金額,、稅額如果輸入了非數(shù)字字符,將其返回原來的值,。4,、UserForm1
(1)添加控件:FrmHeader 插入一個(gè)框架控件:FrmHeader 在FrmHeader中,添加一個(gè)標(biāo)簽控件:LbTopAmount
(2)UserForm_Initialize(),,窗體初始化過程 Dim clsTxB As New TextBoxEventHandler Dim textBoxes As Collection Private Sub UserForm_Initialize() Dim tbTitle() tbTitle = Array("ID", "報(bào)銷單號(hào)", "開票日期", "發(fā)票號(hào)碼", "銷售方全稱", _ "開票項(xiàng)目", "金額", "稅額", "價(jià)稅合計(jì)", "購買方全稱", "購買方稅號(hào)", "銷售方稅號(hào)", _ "發(fā)票代碼", "發(fā)票類型", "登記人", "報(bào)銷人", "審核人", "登記日期", "報(bào)銷日期", _ "報(bào)銷項(xiàng)目", "發(fā)票源文件地址", "發(fā)票文件路徑", "備注", "付款人", "記賬人", _ "月份", "會(huì)計(jì)憑證號(hào)", "部門", "發(fā)票原文件名", "電子票號(hào)", "附件文件路徑") arrWidth = Array(0, 0, 60, 0, 100, 120, 80, 60, 90, 0, 0, 120, 0, 0, _ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 125, 0, _ 0, 0, 0, 0, 0, 0, 0, 0, 0) For i = 0 To UBound(tbTitle, 1) - 1 Set lbCtrl = Me.FrmHeader.Controls.Add("Forms.Label.1", "topLb_" & i, True) Set txtBox = Me.FrmHeader.Controls.Add("Forms.TextBox.1", "topTb_" & i, True) If i = 0 Then iWidth = 0 Else iWidth = iWidth + arrWidth(i - 1) End If With lbCtrl .Caption = tbTitle(i) .Height = 20 .Top = 0 .Width = arrWidth(i) .Left = iWidth .BorderStyle = 1 .FontSize = 9 .FontName = "微軟雅黑" .ForeColor = RGB(50, 50, 255) .TextAlign = 2 .ZOrder (0) End With With txtBox .Height = 20 .Top = lbCtrl.Height .Width = arrWidth(i) .Left = iWidth .BorderStyle = 1 .FontSize = 9 .FontName = "微軟雅黑" .ForeColor = RGB(50, 50, 255) If i = Pxy(tbTitle, "金額") - 1 Or i = Pxy(tbTitle, "稅額") - 1 _ Or i = Pxy(tbTitle, "價(jià)稅合計(jì)") - 1 Then .TextAlign = fmTextAlignRight .Text = 0 Else .TextAlign = fmTextAlignLeft End If .ZOrder (0) End With clsTxB.ReceiveTextbox txtBox '將 TextBoxEventHandler 對(duì)象添加到集合中 If textBoxes Is Nothing Then Set textBoxes = New Collection End If textBoxes.Add clsTxB Set clsTxB = Nothing Next With Me.FrmHeader .Caption = "" .Width = iWidth .Height = 40 .Left = 1 .Top = 30 .Visible = True End With Set iCtr = Me.Controls("TopTb_" & Pxy(tbTitle, "價(jià)稅合計(jì)") - 1) With Me.LbTopAmount .Caption = "" .Top = iCtr.Top .Left = iCtr.Left .Height = iCtr.Height .Width = iCtr.Width .ZOrder 0 End With End Sub
代碼解析:
(A)聲明一個(gè)名為 clsTxB 的 TextBoxEventHandler 類的實(shí)例,,用于處理文本框事件。(B)聲明一個(gè)集合變量 textBoxes,,用于存儲(chǔ) TextBoxEventHandler 對(duì)象,。(C)tbTitle 數(shù)組,存放標(biāo)簽的標(biāo)題,。(D)循環(huán)tbTitle 數(shù)組,,添加標(biāo)簽,、文本框并設(shè)置格式。(E)調(diào)用 TextBoxEventHandler 類的 ReceiveTextbox 子過程,,將當(dāng)前文本框?qū)ο髠鬟f給它,。將 clsTxB 添加到 textBoxes 集合中,用于后續(xù)引用,。(F)對(duì)FrmHeader 框架進(jìn)行屬性設(shè)置,。(G)對(duì)LbTopAmount標(biāo)簽進(jìn)行設(shè)置,位置,、大小與價(jià)稅合計(jì)標(biāo)簽的對(duì)應(yīng)的文本框相同,,就是完全覆蓋在其上,達(dá)到禁止修改價(jià)稅合計(jì)的目的,。
|