简体   繁体   English

如果单元格与列中的某个值匹配,则在相邻列中插入数据

[英]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.

相关问题 如果相邻单元格值与列名匹配,则 Excel VBA 在列下插入行 - Excel VBA insert rows under the column if adjacent cell value matches column names 如果单元格与任一列中的单元格匹配,则返回相邻的单元格文本 - If Cell Matches Cell in Either Column Return Adjacent Cell Text 当列匹配某些关键字时,Python插入值 - Python Insert value when Column matches certain keywords 在Excel中,如果相邻列具有特定值,如何计算列中的空白单元格? - In Excel, how do you count a blank cell in a column if an adjacent column has a certain value? VBA-对多个行求和,其中相邻列的值与另一个单元格匹配 - VBA - Sum multiple rows where adjacent column value matches another cell 在列中查找值并在相邻单元格中输出 - Find value in column and output in adjacent cell 按相邻单元格值合并或连接列 - Merge or concatenate column by adjacent cell value 根据相邻列中的值更改单元格颜色 - Change cell colour according to value in adjacent column 在列中查找具有特定值的第一个单元格,然后在上方插入3行 - Look for first cell with certain value in column, then insert 3 rows above VBA:如何基于包含某些字符的相邻单元格删除列? - VBA: How to delete a column based on the adjacent cell containing certain characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM