简体   繁体   English

使用用户表单更新Excel工作表数据

[英]Updating excel worksheet data with userform

I am trying to enter in information in a database that I have in excel using a userform. 我正在尝试使用用户窗体在excel中具有数据库的信息中输入信息。 The catch here is that I need to be able to enter data into a specific cell using a text box. 这里要注意的是,我需要能够使用文本框将数据输入到特定的单元格中。 For example, I open the userform and in textbox1 I type 123 in textbox2 I type 321. When I hit the submit button in the userform the code should look for the text "123" in a specific column. 例如,我打开用户窗体,然后在textbox1中输入123,在textbox2中键入321。当我在用户窗体中单击“提交”按钮时,代码应在特定列中查找文本“ 123”。 If the text "123" exists in the column specified in the code then it should take the data entered in textbox2(321) and put it in a cell adjacent to the cell containing the text 123 如果代码中指定的列中存在文本“ 123”,则应获取在textbox2(321)中输入的数据,并将其放在与包含文本123的单元格相邻的单元格中

I have combed the internet looking for a solution to this and only found one that only sort of worked. 我梳理了互联网,寻找解决方案,但只找到了一种可行的方法。

This is the link to that solution: https://youtu.be/cKKgYPfq3_I 这是该解决方案的链接: https : //youtu.be/cKKgYPfq3_I

The code wouldn't work consistently though. 该代码将无法始终如一地工作。 I don't know if this is because some of the edits I made to the code in this video caused the errors or if the code itself had any errors. 我不知道这是否是因为我对本视频中的代码所做的一些编辑导致了错误,或者代码本身是否有任何错误。

This will be for a database that will have a ton of information on it. 这将用于具有大量信息的数据库。 Having a function like this would make it a lot easier for someone to update information on the database. 具有这样的功能将使某人更容易更新数据库上的信息。 You could have for example a list of phone numbers and names. 例如,您可能具有电话号码和名称的列表。 With this function it would allow you to enter a name in one text box and a new phone number in the second text box. 使用此功能,您可以在一个文本框中输入名称,在第二个文本框中输入新的电话号码。 You could then update an individuals phone number when pushing the submit button. 然后,您可以在按下“提交”按钮时更新个人电话号码。

Any suggestions would be a huge help. 任何建议都会有很大帮助。

Thanks in advance! 提前致谢!

Below is what I have so far that I found from a different forum and edited from some help that I got from here. 以下是到目前为止,我在另一个论坛中找到的内容,并根据从此处获得的一些帮助进行了编辑。 It doesn't seem to work. 它似乎不起作用。 Then again it could be because the code wasn't at all intended for what I would like it to do. 再说一遍,可能是因为代码根本不符合我想要的目的。 (My knowledge of VBA is limited to puzzle piecing code together that I can find that seems to work.) (我对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

Please try this (we suppose that our sheet is the sheet2) : 请尝试以下操作(我们假设我们的工作表是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM