2017-09-27 38 views
0

我想输入数据库中的信息,我使用用户窗体在Excel中输入。这里的关键是我需要能够使用文本框将数据输入到特定的单元格中。例如,我打开用户窗体并在textbox1中输入textbox2中的123我输入321。当我点击用户窗体中的提交按钮时,代码应在特定列中查找文本“123”。如果在代码中指定的列中存在文本“123”,那么它应该将输入到textbox2(321)中的数据放到与包含文本123的单元格相邻的单元格中。我已经梳理了互联网,对于这个解决方案,只发现只有一种工作。使用用户窗体更新excel工作表数据

这是链接到该解决方案: 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

欢迎到SO;告诉我们你试过的东西!你发现那种“有效的”是什么?你应该阅读关于如何提出一个好问题的[帮助]。 – user3788685

+0

雅谢谢!我也注意到了,但是找不到编辑按钮。 – Kam

+0

@ user3788685因此,我一直在为此工作几个月。我忘记了原始解决方案的来源。我对VBA并不陌生,我可以通过阅读代码和拼图代码来创建适用于我的程序,但这并不意味着我总是能够100%理解即时消息的内容。我一直在使用的代码在不同的论坛上发现,我甚至不确定我是否正确阅读它。 Ill立即张贴 – Kam

回答

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 
+0

这是完美的。如果有人正在准备更新工作表中的某些数据并打开用户表单,并意外在文本框1或文本框2中输入了错字,那么可能会弹出一个消息框,提示类似“找不到数据”的内容,以便用户会知道他打算更新的数据没有正确更新?现在,如果您输入错字,用户表单就会关闭,并且无法知道您打算更新的内容是否已正确更新。 – Kam

+0

我已更新您的请求的代码 –

+0

非常感谢!该程序的工作原理除了每次执行程序时,无论textbox1和textbox2中的信息是否正确,我都会收到消息“在某些情况下数据未找到!!,请检查日期是否带有红色字体”。 – Kam

相关问题