我正在尝试使用userform在excel中的数据库中输入信息。这里的问题是我需要能够使用文本框将数据输入特定单元格。例如,我打开userform,在textbox1中我在textbox2中输入123我输入321.当我点击userform中的提交按钮时,代码应该查找文本" 123"在特定列中。如果文字" 123"存在于代码中指定的列中,然后它应该采用在textbox2(321)中输入的数据并将其放在与包含文本123的单元格相邻的单元格中
我已经梳理了互联网,正在寻找解决方案,并且只发现了一种只能工作的解决方案。
这是该解决方案的链接: https://youtu.be/cKKgYPfq3_I
但代码不会一致地工作。我不知道这是否是因为我对此视频中的代码所做的一些编辑导致错误或代码本身有任何错误。
这将是一个有大量信息的数据库。拥有这样的功能会使某人更容易更新数据库上的信息。例如,您可以获得电话号码和姓名列表。使用此功能,您可以在一个文本框中输入名称,在第二个文本框中输入新的电话号码。然后,您可以在按下提交按钮时更新个人电话号码。
任何建议都将是一个巨大的帮助。
提前致谢!
以下是我到目前为止所发现的,我从一个不同的论坛发现,并从我从这里得到的一些帮助编辑。它似乎不起作用。然后又可能是因为代码根本不打算用于我想做的事情。 (我对VBA的了解仅限于拼凑拼图代码,我发现它似乎有效。)
Option Explicit
Private Sub CommandButton1_Click()
Dim WS As Worksheet
Dim lastrow As Long
Dim r As Long
Dim datee As Integer
Dim m As Integer
Application.ScreenUpdating = False
m = 1
If IsNumeric(TextBox2.Text) Then
On Error GoTo ErrorHandler
datee = TextBox2.Text
Set WS = ActiveWorkbook.Worksheets("Scrap")
lastrow = WS.Cells(Rows.Count, "A").End(xlUp).Row
For r = 1 To lastrow
If WS.Cells(r, 2) = TextBox1.Text Then
WS.Cells(r, 5).Value = TextBox2.Text
WS.Cells(r, 8).Value = TextBox3.Text
WS.Cells(r, 9).Value = TextBox4.Text
WS.Cells(r, 10).Value = TextBox5.Text
WS.Cells(r, 11).Value = TextBox6.Text
WS.Cells(r, 12).Value = TextBox7.Text
WS.Cells(r, 13).Value = TextBox8.Text
WS.Cells(r, 14).Value = TextBox9.Text
WS.Cells(r, 15).Value = TextBox10.Text
WS.Cells(r, 16).Value = TextBox11.Text
WS.Cells(r, 17).Value = TextBox12.Text
WS.Cells(r, 18).Value = TextBox13.Text
WS.Cells(r, 19).Value = TextBox14.Text
Else
WS.Cells(r, 2).Font.Color = vbRed
m = 0
End If
Next
If m = 0 Then
MsgBox "Sales Order number not found,make sure the Sales Order Number you entered is correct", vbCritical
Else
MsgBox "Success", vbInformation
End If
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
TextBox11.Text = ""
TextBox12.Text = ""
TextBox13.Text = ""
TextBox14.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
End If
MsgBox "Please Insert Data", vbCritical
End Sub
答案 0 :(得分:0)
请尝试这个(我们假设我们的工作表是sheet2):
Option Explicit
Private Sub CommandButton1_Click()
Dim WS As Worksheet
Dim lastrow As Long
Dim r As Long
Dim datee As Date
Application.ScreenUpdating = False
If IsDate(TextBox2.Text) Then
On Error GoTo ErrorHandler
datee = TextBox2.Text
Set WS = ActiveWorkbook.Worksheets("sheet2")
lastrow = WS.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
If WS.Cells(r, 3) = TextBox1.Text And WS.Cells(r, 2) = datee Then
WS.Cells(r, 4).Value = TextBox2.Text
WS.Cells(r, 5).Value = TextBox3.Text
WS.Cells(r, 2).Font.Color = vbRed
WS.Cells(r, 3).Font.Color = vbRed
WS.Cells(r, 4).Font.Color = vbRed
WS.Cells(r, 5).Font.Color = vbRed
MsgBox "Success", vbInformation
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
End If
Next
MsgBox "Data not Found!!", vbCritical
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
Exit Sub
End If
MsgBox "Please Insert Date in the Date Box", vbCritical
Application.ScreenUpdating = True
End Sub