我是 vba 新手,我想学习。请帮我解决以下问题:
我有这个应用程序(见图),我用它在工作表中输入数据(与手动完成相比,它更容易使用)。它基本上是一个库存管理系统。当我在那里添加产品时,我可以选择出售或购买。
我想实现的下一件事是在我想添加销售时自动填充表单(考虑到它首先是作为购买添加的),例如基于序列号。这将非常有用,因为当我在数据库中输入销售时,我不必再次填写所有文本框
您对我如何做到这一点有任何想法吗?
亲切的问候,特赖安。
所以,基本上我不应该提供帮助,因为你还没有完成你的研究,但我确实觉得看看我是否可以创建这样的功能很有趣。
您将无法简单地粘贴代码,但它确实可以作为自动填充。
这是我用于自动填充的“数据”源,它正在寻找左侧值并将自动完成该文本框,以及具有 C 列值的辅助文本框。这将适用于 n 个自动填充。
我只使用了 2 个不同的字段来测试这个想法,忽略标签。这就是不输入任何内容的样子。
一旦你开始输入,“自动填充”就会出现。
如果您将“悬停”在自动填充上,它将变成不同的颜色,以及所有输入表,输入表现在还包括自动填充的答案。如果您要“取消悬停”(悬停在除自动填充之外的任何内容上),它将恢复到第二张图片。
如果我要为一个真正的项目再次编写这段代码,我会改变一些事情。
这是代码:
Private Sub Autofill_Click()
Dim BestOption As Integer
Dim ValueRange As Range
Set ValueRange = Sheets("sheet1").Range("B8:B13")
Dim Start As Range
Set Start = Sheets("sheet1").Range("B7")
BestOption = WorksheetFunction.Match(Autofill, ValueRange, 0)
TextBox2 = Start.Offset(BestOption, 1)
TextBox1 = Start.Offset(BestOption, 0)
Autofill.Visible = False
TextBox3.Visible = False
TextBox4.Visible = False
End Sub
Private Sub TextBox1_Change()
Dim Start As Range
Dim ValueRange As Range
Dim MatchCounter As Integer
Set Start = Sheets("sheet1").Range("B7")
Set ValueRange = Sheets("sheet1").Range("B8:B13")
If TextBox1 = "" Then
Autofill.Visible = False
Else
'Call FindClosestMatch(TextBox1)
Autofill.Visible = True
Autofill = Start.Offset(FindClosestMatch(TextBox1) + 1, 0)
End If
End Sub
Function FindClosestMatch(Entry As String) As Integer
Dim BestOption As Integer
Dim Start As Range
Set Start = Sheets("sheet1").Range("B7")
Dim MyArray(6) As String
Dim i As Integer
Dim j As Integer
Dim iChar As String
Dim EntryChar As String
For i = 0 To 5
MyArray(i) = Start.Offset(i + 1, 0)
Next i
For j = 1 To Len(Entry)
EntryChar = Mid(Entry, j, 1)
For i = 0 To 5
If EntryChar = "" Then
Exit For
End If
iChar = Mid(MyArray(i), j, 1)
If iChar = EntryChar Then
BestOption = i
Else
MyArray(i) = "................."
End If
Next i
Next j
FindClosestMatch = BestOption
End Function
'hover
Private Sub Autofill_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Autofill.BackColor = &H80000002
TextBox3.BackColor = &H80000002
TextBox4.BackColor = &H80000002
Dim BestOption As Integer
Dim ValueRange As Range
Set ValueRange = Sheets("sheet1").Range("B8:B13")
Dim Start As Range
Set Start = Sheets("sheet1").Range("B7")
BestOption = WorksheetFunction.Match(Autofill, ValueRange, 0)
TextBox3.Visible = True
TextBox4.Visible = True
TextBox4 = Start.Offset(BestOption, 1)
TextBox3 = Start.Offset(BestOption, 0)
End Sub
Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call test
End Sub
Private Sub TextBox2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call test
End Sub
Private Sub TextBox3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call test
End Sub
Private Sub TextBox4_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call test
End Sub
Private Sub UserForm_Click()
Call test
Autofill.Visible = False
End Sub
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call test
End Sub
Sub test()
Autofill.BackColor = &H80000000
TextBox1.BackColor = &H80000005
TextBox2.BackColor = &H80000005
TextBox3.Visible = False
TextBox4.Visible = False
End Sub
需要思考的问题:
注意:我使用了 4 个文本框,数字 1 和 2 彼此重叠,数字 2 和 4 彼此重叠。如果您不小心将鼠标悬停在自动填充上,这样做不会丢失已经输入的输入。