[英]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
中的值时,它会自动填充到该特定ID
的Completed
的所有单元格中? 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.在下面的数据中,我希望三个空白单元格分别自动填充
4
、 6
和5
。
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 列中的状态,它将在公式列中更新。
What does it do?它有什么作用?
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用法
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
"B"
and "C"
you can use the numbers 2
and 3
.2
和3
而不是"B"
和"C"
。SourceColumn
, TargetColumn
and FirstRow
.SourceColumn
、 TargetColumn
和FirstRow
的参数。Me
and Target
stay the same. Me
和Target
保持不变。 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.