您可以通過以下方式支持我:1、關注、點贊、留言、分享、打賞;2、點擊感興趣的廣告、購買我的安利微店產品;3、添加我的合谷醫(yī)療企業(yè)微信,謝謝! ☆快速瀏覽☆【2023年3月合集】【2023年4月合集】【2023年5月合集】【2023年6月合集】 ☆本期內容概要☆ 完整代碼: 1、Sheet1_CmdShow (命令按鈕) Private Sub CmdShow_Click() UserForm1.Show End Sub
代碼解析:
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ù)組最大下標,再使之等于1-t,作用是修正數(shù)組最小下標不為1時對應的位置值。(B)通過i循環(huán)數(shù)組,比對查找值與數(shù)組中的值,若找到,則函數(shù)值等于t+1。3、類模塊(TextBoxEventHandler)
(1)定義變量 Public WithEvents txtBox As MSForms.TextBox Private previousValue As String
代碼解析:
(A)聲明一個公共變量 txtBox,它是一個帶有事件處理程序的文本框對象。(B)聲明一個私有變量 previousValue,用于存儲文本框在更新前的值。(2)ReceiveTextbox Public Sub ReceiveTextbox(ByVal reTextbox As MSForms.TextBox) Set txtBox = reTextbox End Sub
代碼解析:
(A)定義一個公共子過程 ReceiveTextbox,用于接收一個文本框對象,并將其賦值給 txtBox 變量。。(B)將傳入的文本框對象賦值給 txtBox 變量。(3)txtBox_Change,文本框(TextBox)相關的事件處理程序 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 "請輸入數(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 "請輸入數(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 = "價稅合計" Then .Controls("topTb_" & i).Text = CDbl(.Controls("topTb_" & i - 2)) + CDbl(.Controls("topTb_" & i - 1)) End If End With End Sub
代碼解析:如果文本框發(fā)生了改變,激活其Change事件。
(A)如果當前文本框對應的標簽是金額,限制僅輸入數(shù)字,同時價稅合計重新計算=金額+稅額。如果當前文本框對應的標簽是稅額,操作同“金額“。(B)如果當前文本框對應的標簽是價稅合計,則將其重新計算=金額+稅額,直觀看來,不管輸入什么,價稅合計總保持不變,實現(xiàn)價稅合計不可編輯的目的。(4)txtbox_KeyDown Private Sub txtbox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) ' 在按下任意鍵之前,存儲當前值 previousValue = txtBox.Value End Sub
代碼解析:
(A)在按下任意鍵之前,將當前文本框的值存儲在 previousValue 變量中,供后續(xù)調用。(B)這樣做的目的:金額、稅額如果輸入了非數(shù)字字符,將其返回原來的值。4、UserForm1
(1)添加控件:FrmHeader 插入一個框架控件:FrmHeader 在FrmHeader中,添加一個標簽控件:LbTopAmount
(2)UserForm_Initialize(),窗體初始化過程 Dim clsTxB As New TextBoxEventHandler Dim textBoxes As Collection Private Sub UserForm_Initialize() Dim tbTitle() tbTitle = Array("ID", "報銷單號", "開票日期", "發(fā)票號碼", "銷售方全稱", _ "開票項目", "金額", "稅額", "價稅合計", "購買方全稱", "購買方稅號", "銷售方稅號", _ "發(fā)票代碼", "發(fā)票類型", "登記人", "報銷人", "審核人", "登記日期", "報銷日期", _ "報銷項目", "發(fā)票源文件地址", "發(fā)票文件路徑", "備注", "付款人", "記賬人", _ "月份", "會計憑證號", "部門", "發(fā)票原文件名", "電子票號", "附件文件路徑") 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, "價稅合計") - 1 Then .TextAlign = fmTextAlignRight .Text = 0 Else .TextAlign = fmTextAlignLeft End If .ZOrder (0) End With clsTxB.ReceiveTextbox txtBox '將 TextBoxEventHandler 對象添加到集合中 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, "價稅合計") - 1) With Me.LbTopAmount .Caption = "" .Top = iCtr.Top .Left = iCtr.Left .Height = iCtr.Height .Width = iCtr.Width .ZOrder 0 End With End Sub
代碼解析:
(A)聲明一個名為 clsTxB 的 TextBoxEventHandler 類的實例,用于處理文本框事件。(B)聲明一個集合變量 textBoxes,用于存儲 TextBoxEventHandler 對象。(C)tbTitle 數(shù)組,存放標簽的標題。(D)循環(huán)tbTitle 數(shù)組,添加標簽、文本框并設置格式。(E)調用 TextBoxEventHandler 類的 ReceiveTextbox 子過程,將當前文本框對象傳遞給它。將 clsTxB 添加到 textBoxes 集合中,用于后續(xù)引用。(G)對LbTopAmount標簽進行設置,位置、大小與價稅合計標簽的對應的文本框相同,就是完全覆蓋在其上,達到禁止修改價稅合計的目的。
|