[英]If cell matches a certain value in a column, insert data in an adjacent column
So I have two forms in Sheet EmployeeForm
(EmployeeForm1 & EmployeeForm2) and an Excel Table TableEmployee
in Sheet EmployeeData
that looks like this:所以,我有两种形式的表
EmployeeForm
(EmployeeForm1&EmployeeForm2)和Excel表格TableEmployee
在表EmployeeData
,看起来像这样:
The data in the table comes from the submission from these two forms, but so far I've only succeeded in inputting the first half of the table.表格中的数据来自这两个表格的提交,但到目前为止我只成功输入了表格的前半部分。
The data in Employee Form 2
is submitted only and only after the Employee Form 1
is submitted (can be days, even weeks later). Employee Form 2
的数据仅在提交Employee Form 1
之后提交(可能是几天,甚至几周之后)。
Now what I want to achieve is to have a working VBA code that can match the Employee ID
in cell D13 with Employee ID
in column H correctly, and record the data in D14:D17 to its proper place.现在我想实现的是有一个工作的VBA代码,可以匹配的
Employee ID
与小区D13 Employee ID
在H列正常,并记录在D14的数据:到适当的位置D17。
So in this example above, since the Employee ID
is 145, once I click the submit button in Form 2, the data in D14:D17 should be stored in L7:O7.所以在上面的这个例子中,由于
Employee ID
是 145,一旦我单击 Form 2 中的提交按钮,D14:D17 中的数据应该存储在 L7:O7 中。
This is my code so far:到目前为止,这是我的代码:
Sub Submit_Form1()
Dim LastRow As Long, ws As Worksheet
Set ws = Worksheets("EmployeeData")
LastRow = ws.Range("H" & Rows.Count).End(xlUp).Row + 1
ws.Range("H" & LastRow).Value = Worksheets("EmployeeForm").Range("D5").Value 'Employee ID
ws.Range("I" & LastRow).Value = Worksheets("EmployeeForm").Range("D6").Value 'Employee Name
ws.Range("J" & LastRow).Value = Worksheets("EmployeeForm").Range("D7").Value 'Place of Birth
ws.Range("K" & LastRow).Value = Worksheets("EmployeeForm").Range("D8").Value 'Working Experience
End Sub
And for Form 2对于表格 2
Sub Submit_Form2()
Dim LastRow As Long, ws As Worksheet
Dim H As String
Set ws = Worksheets("EmployeeData")
employeeid = Sheets("EmployeeForm").Range("D13").Value
If Cells(H) = employeeid Then
ws.Range("L" & LastRow).Value = Worksheets("EmployeeForm").Range("D14").Value 'Education
ws.Range("M" & LastRow).Value = Worksheets("EmployeeForm").Range("D15").Value 'Last Company
ws.Range("N" & LastRow).Value = Worksheets("EmployeeForm").Range("D16").Value 'Join Date
ws.Range("O" & LastRow).Value = Worksheets("EmployeeForm").Range("D17").Value 'Position
End Sub
Of course, the second macro doesnt work, but can anybody please enlighten me as how to do this the right way?当然,第二个宏不起作用,但有人可以请教我如何正确地做到这一点吗? Thanks a lot!
非常感谢!
Can you try this?你能试试这个吗?
Sub Submit_Form2()
Dim ws As Worksheet, v As Variant
Set ws = Worksheets("EmployeeData")
employeeid = Sheets("EmployeeForm").Range("D13").Value
v = Application.Match(employeeid, ws.Range("H:H"), 0)
If IsNumeric(v) Then
ws.Range("L" & v).Value = Worksheets("EmployeeForm").Range("D14").Value 'Education
ws.Range("M" & v).Value = Worksheets("EmployeeForm").Range("D15").Value 'Last Company
ws.Range("N" & v).Value = Worksheets("EmployeeForm").Range("D16").Value 'Join Date
ws.Range("O" & v).Value = Worksheets("EmployeeForm").Range("D17").Value 'Position
End If
End Sub
The problem with your code was你的代码的问题是
If Cells(H) = employeeid Then
which is not valid syntax.这是无效的语法。 Cells needs a row and column reference such cells(1,1) or cells (1,"A").
单元格需要行和列引用,例如单元格 (1,1) 或单元格 (1,"A")。 Not to mention that H wasn't defined.
更不用说 H 没有定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.