简体   繁体   English

Excel 公式根据另一列中的值填充一列

[英]Excel formula to fill a column based on value in another column

How can I make a formula such that when update a value in Completed for a particular value of ID , it automatically gets filled in all cells of Completed for that particular ID ?如何制作一个公式,以便在为特定ID值更新Completed中的值时,它会自动填充到该特定IDCompleted的所有单元格中? And, when I remove the value from one cell in Completed , it automatically gets removed from all cells in Completed that correspond to that value in ID .而且,当我从Completed中的一个单元格中删除该值时,它会自动从Completed中与ID中的该值对应的所有单元格中删除。

For eg.例如。 in the data below, I'd like the three blank cells automatically filled with 4 , 6 and 5 respectively.在下面的数据中,我希望三个空白单元格分别自动填充465

Role      ID      Completed
 A         1          3
 A         2          4
 A         5          3
 A         8          6
 B         2          
 B         8
 B        10          5
 C        10           
 C        15          2 

Here is one approach.这是一种方法。

  • Your setup is in column A, B & C您的设置在 A、B 和 C 列中

  • Prepare a LOOKUP table in column E & F as shown below.在 E & F 列中准备一个 LOOKUP 表,如下所示。

     ID Completed 1 3 2 4 5 3 8 6 10 5 15 2

Then in column C (cell C2), you can use a simple formula like below and copy down as much needed.然后在 C 列(单元格 C2)中,您可以使用如下所示的简单公式并根据需要复制下来。

=IFERROR(VLOOKUP(B2,$E:$F,2,0),"")

So, as soon as you update status in column E & F, it will get updated in the formula column.因此,一旦您更新 E 和 F 列中的状态,它将在公式列中更新。

A Worksheet Change Solution工作表更改解决方案

What does it do?它有什么作用?

  • When a value in Target Column is changed to a new value , the value in the same row of Source Column is being looked up in the same Source Column .Target Column中的值更改为新值时,Source Column 同一行中的值正在同一Source Column Source Column中查找。 With each found value, the value in this (found) row in Target Column is changed to the mentioned new value .对于每个找到的值, Target Column中此(找到)行中的值将更改为提到的新值

Usage用法

  • To run the following successfully, both codes have to be copied to one workbook appropriately: the first to a sheet module and the second to a standard module.要成功运行以下代码,必须将两个代码适当地复制到一个工作簿:第一个复制到工作模块,第二个复制到标准模块。
  • There is nothing to run here, everything runs automatically .这里没有什么可以运行的,一切都自动运行。
  • The only thing that could be changed are the last three values in the short code.唯一可以更改的是短代码中的最后三个值

The Two Codes两个密码

1. Sheet Module 1.图纸模块

The following code is to be copied into a sheet module eg Sheet1以下代码将被复制到工作模块中,例如Sheet1

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    updateColumn Me, Target, "B", "C", 2
End Sub
  • Instead of "B" and "C" you can use the numbers 2 and 3 .您可以使用数字23而不是"B""C"
  • You can change the values as you see fit.您可以根据需要更改值。
  • You can copy it into multiple sheet modules and change the parameters for SourceColumn , TargetColumn and FirstRow .您可以将其复制到多个工作表模块并更改SourceColumnTargetColumnFirstRow的参数。
  • Me and Target stay the same. MeTarget保持不变。

2. Standard Module 2.标准模块

The following code is to be copied into a standard module eg Module1以下代码将被复制到标准模块中,例如Module1

Option Explicit

Sub updateColumn(Sheet As Worksheet, _
                 TargetCell As Range, _
                 ByVal SourceColumn As Variant, _
                 ByVal TargetColumn As Variant, _
                 Optional ByVal FirstRow As Long = 4)
    
    If TargetCell.Cells.CountLarge > 1 Then GoTo MoreThanOneCell
    
    Dim rng As Range: Set rng = Sheet.Columns(TargetColumn)
    If Intersect(TargetCell, rng) Is Nothing Then GoTo NotInTargetColumn
    
    Set rng = rng.Find("*", , xlValues, , , xlPrevious)
    If rng Is Nothing Then GoTo EmptyTargetColumn
    If rng.Row < FirstRow Then GoTo FirstRowBelowLastRow
    
    Dim LastRow As Long: LastRow = rng.Row
    Set rng = Sheet.Columns(SourceColumn).Find("*", , xlValues, , , xlPrevious)
    If Not rng Is Nothing Then
        If rng.Row > LastRow Then LastRow = rng.Row
    Else ' Empty Source Column. Don't care.
    End If
    If FirstRow = LastRow Then GoTo OnlyOneCell
    
    Set rng = Sheet.Range(Sheet.Cells(FirstRow, TargetColumn), _
                          Sheet.Cells(LastRow, TargetColumn))
    If Intersect(TargetCell, rng) Is Nothing Then GoTo NotInTargetRange
    
    Dim ColOff As Long: ColOff = Sheet.Columns(SourceColumn).Column - rng.Column
    Dim Target As Variant: Target = rng.Value
    Dim Source As Variant: Source = rng.Offset(, ColOff).Value
        
    Dim i As Long, tVal As Variant, sVal As Variant
    tVal = TargetCell.Value
    sVal = TargetCell.Offset(, ColOff).Value
    Debug.Print TargetCell.Address, tVal, _
                TargetCell.Offset(, ColOff).Address, sVal
    On Error GoTo CleanExit
    For i = 1 To UBound(Source)
        If Source(i, 1) = sVal Then
            Target(i, 1) = tVal
        End If
    Next i
    'Application.EnableEvents = False
    rng.Value = Target
    
CleanExit:
   ' Application.EnableEvents = True
LastExit:
    Exit Sub

MoreThanOneCell:
    'Debug.Print "More than one cell."
    GoTo LastExit
NotInTargetColumn:
    'Debug.Print "Not in Target Column."
    GoTo LastExit
EmptyTargetColumn:
    'Debug.Print "Empty Target Column."
    GoTo LastExit
FirstRowBelowLastRow:
    'Debug.Print "First row below last row."
    GoTo LastExit
OnlyOneCell:
    'Debug.Print "Only one cell."
    GoTo LastExit
NotInTargetRange:
    'Debug.Print "Not in Target Range."
    GoTo LastExit
    
End Sub

You can uncomment the Debug.Print lines to monitor the behavior of the Change event in the Immediate window ( CTRL + G ) in VBE ( Alt + F11 ).您可以取消注释Debug.Print行以监视VBE ( Alt + F11 ) 中Immediate window ( CTRL + G ) 中的Change event的行为。

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

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