Excel:通过另一个列ID充实列数据

Excel:通过另一个列ID充实列数据,excel,Excel,我有一个excel文件,其中有多行具有相同的“主题ID”,其中一列(值a)始终具有相同的主题ID值(或为空)。例如: ╔══════════════════════════╗ ║ | subject id | value A | ║ ╠══════════════════════════╣ ║ |:----------:|---------| ║ ║ | 1 | A | ║ ║ | 1 | A | ║ ║ | 1 |

我有一个excel文件,其中有多行具有相同的“主题ID”,其中一列(值a)始终具有相同的主题ID值(或为空)。例如:

╔══════════════════════════╗
║ | subject id | value A | ║
╠══════════════════════════╣
║ |:----------:|---------| ║
║ |      1     |    A    | ║
║ |      1     |    A    | ║
║ |      1     |         | ║
║ |      1     |         | ║
║ |      2     |         | ║
║ |      2     |         | ║
║ |      2     |    B    | ║
╚══════════════════════════╝
如何在Excel中创建一个公式,将值向下拖动到列中,使具有相同主题ID的所有行都具有相同的值a?在我的数据中,对于特定的主题ID,在值A列中不会有不同的值:它要么有值,要么没有值

例如,我希望通过这样做,使ID为1的所有对象都得到a值“a”,对于ID为2的每个记录,我希望它得到B值。例如:

╔══════════════════════════╗
║ | subject id | value A | ║
╠══════════════════════════╣
║ |:----------:|---------| ║
║ |      1     |    A    | ║
║ |      1     |    A    | ║
║ |      1     |         | ║
║ |      1     |         | ║
║ |      2     |         | ║
║ |      2     |         | ║
║ |      2     |    B    | ║
╚══════════════════════════╝
╔══════════════════════════╗
║ | subject id | value A | ║
╠══════════════════════════╣
║ |:----------:|---------| ║
║ |      1     |    A    | ║
║ |      1     |    A    | ║
║ |      1     |    A    | ║
║ |      1     |    A    | ║
║ |      2     |    B    | ║
║ |      2     |    B    | ║
║ |      2     |    B    | ║
╚══════════════════════════╝

使用字符使用ASCII代码将数字转换为字符

注意,这个值最多只能工作到26,之后您的字母就用完了,将开始得到括号和其他字符

=CHAR(A2+64)

使用字符使用ASCII代码将数字转换为字符

注意,这个值最多只能工作到26,之后您的字母就用完了,将开始得到括号和其他字符

=CHAR(A2+64)

扩展Dan的解决方案,此解决方案应可用于26个以上的不同ID

=IF(A1>26,CHAR(QUOTIENT(A1-1,26)+64),"") & CHAR(1+MOD(A1-1,26)+64)
其中A1具有ID。该公式给出了以下结果

1   A
2   B
.
.
.
26  Z
27  AA
28  AB
29  AC
.
.
.
44  AR
45  AS
.
.
.
53  BA
54  BB
55  BC

扩展Dan的解决方案,此解决方案应可用于26个以上的不同ID

=IF(A1>26,CHAR(QUOTIENT(A1-1,26)+64),"") & CHAR(1+MOD(A1-1,26)+64)
其中A1具有ID。该公式给出了以下结果

1   A
2   B
.
.
.
26  Z
27  AA
28  AB
29  AC
.
.
.
44  AR
45  AS
.
.
.
53  BA
54  BB
55  BC
以下是我的结果:

+------------+---------+----------------+
| subject id | value A | Formula result |
+------------+---------+----------------+
|          1 |         | Hello          |
|          1 |         | Hello          |
|          1 | Hello   | Hello          |
|          1 |         | Hello          |
|          1 |         | Hello          |
|          1 |         | Hello          |
|          2 |         | B              |
|          2 | B       | B              |
|          2 | B       | B              |
|          2 |         | B              |
|          2 |         | B              |
|          2 | B       | B              |
|          3 |         | World          |
|          3 |         | World          |
|          3 | World   | World          |
|          3 |         | World          |
|          3 |         | World          |
|          4 |         | D              |
|          4 | D       | D              |
|          4 |         | D              |
|          4 |         | D              |
+------------+---------+----------------+
我为它构建了一个UDF,C2中的公式是=GetCode(A2,$a$2:$a$22,1),其中第一个参数是要搜索的值,第二个是要搜索的范围,最后一个是要偏移以查找结果的列

代码如下:

Function GetCode(InputValue As String, SearchRange As Range, ColOffset As Long)
Dim TempVal As String, FirstFoundRow As Long, LastFoundRow As Long, X As Long
On Error Resume Next 'If not found the result will be null
TempVal = Null
If WorksheetFunction.CountIf(SearchRange, InputValue) > 0 Then
    FirstFoundRow = SearchRange.Find(InputValue).Row 'Determine the rows to check
    LastFoundRow = SearchRange.Find(InputValue, , , , , xlPrevious).Row 'Find the last row, no need to test after that
    For X = FirstFoundRow To LastFoundRow 'Loop the checking rows
        TempVal = SearchRange.Cells(X, ColOffset + 1).Text 'Assign the value of the offset column for the tested row
        If TempVal <> "" Then Exit For 'Quit the loop if it has found the answer
    Next
End If
GetCode = TempVal
End Function
函数GetCode(InputValue为字符串,SearchRange为范围,ColOffset为长) Dim TempVal为字符串,FirstFoundRow为长,LastFoundRow为长,X为长 在错误“继续下一步”时,如果未找到,结果将为空 TempVal=Null 如果工作表function.CountIf(SearchRange,InputValue)>0,则 FirstFoundRow=SearchRange.Find(InputValue).Row'确定要检查的行 LastFoundRow=SearchRange.Find(InputValue,,,xlPrevious).Row'查找最后一行,之后无需测试 对于X=FirstFoundRow到LastFoundRow'循环检查行 TempVal=SearchRange.Cells(X,ColOffset+1).Text'为测试行指定偏移量列的值 如果为TempVal“”,则退出“如果找到答案,则退出循环” 下一个 如果结束 GetCode=TempVal 端函数 假设: 主题ID已排序。如果不是,它将给出错误的结果,那么修改代码以搜索非连续数据中的实例就足够简单了

注意:请尝试用更多的关键的细节来格式化你的问题,之前的所有答案都做出了与我相同的假设(当你有1-a,2-B作为样本数据时,这是一件容易假设的事情),同时参考我的上述假设,我不得不做出排序数据的假设,因为你没有告诉我们

“无代码”选项是复制、删除重复项、按列B排序以删除空白,然后对新创建的矩阵使用VLOOKUP公式。

以下是我的结果:

+------------+---------+----------------+
| subject id | value A | Formula result |
+------------+---------+----------------+
|          1 |         | Hello          |
|          1 |         | Hello          |
|          1 | Hello   | Hello          |
|          1 |         | Hello          |
|          1 |         | Hello          |
|          1 |         | Hello          |
|          2 |         | B              |
|          2 | B       | B              |
|          2 | B       | B              |
|          2 |         | B              |
|          2 |         | B              |
|          2 | B       | B              |
|          3 |         | World          |
|          3 |         | World          |
|          3 | World   | World          |
|          3 |         | World          |
|          3 |         | World          |
|          4 |         | D              |
|          4 | D       | D              |
|          4 |         | D              |
|          4 |         | D              |
+------------+---------+----------------+
我为它构建了一个UDF,C2中的公式是=GetCode(A2,$a$2:$a$22,1),其中第一个参数是要搜索的值,第二个是要搜索的范围,最后一个是要偏移以查找结果的列

代码如下:

Function GetCode(InputValue As String, SearchRange As Range, ColOffset As Long)
Dim TempVal As String, FirstFoundRow As Long, LastFoundRow As Long, X As Long
On Error Resume Next 'If not found the result will be null
TempVal = Null
If WorksheetFunction.CountIf(SearchRange, InputValue) > 0 Then
    FirstFoundRow = SearchRange.Find(InputValue).Row 'Determine the rows to check
    LastFoundRow = SearchRange.Find(InputValue, , , , , xlPrevious).Row 'Find the last row, no need to test after that
    For X = FirstFoundRow To LastFoundRow 'Loop the checking rows
        TempVal = SearchRange.Cells(X, ColOffset + 1).Text 'Assign the value of the offset column for the tested row
        If TempVal <> "" Then Exit For 'Quit the loop if it has found the answer
    Next
End If
GetCode = TempVal
End Function
函数GetCode(InputValue为字符串,SearchRange为范围,ColOffset为长) Dim TempVal为字符串,FirstFoundRow为长,LastFoundRow为长,X为长 在错误“继续下一步”时,如果未找到,结果将为空 TempVal=Null 如果工作表function.CountIf(SearchRange,InputValue)>0,则 FirstFoundRow=SearchRange.Find(InputValue).Row'确定要检查的行 LastFoundRow=SearchRange.Find(InputValue,,,xlPrevious).Row'查找最后一行,之后无需测试 对于X=FirstFoundRow到LastFoundRow'循环检查行 TempVal=SearchRange.Cells(X,ColOffset+1).Text'为测试行指定偏移量列的值 如果为TempVal“”,则退出“如果找到答案,则退出循环” 下一个 如果结束 GetCode=TempVal 端函数 假设: 主题ID已排序。如果不是,它将给出错误的结果,那么修改代码以搜索非连续数据中的实例就足够简单了

注意:请尝试用更多的关键的细节来格式化你的问题,之前的所有答案都做出了与我相同的假设(当你有1-a,2-B作为样本数据时,这是一件容易假设的事情),同时参考我的上述假设,我不得不做出排序数据的假设,因为你没有告诉我们


“无代码”选项是复制、删除重复项、按B列排序以删除空白,然后对新创建的矩阵使用VLOOKUP公式。

这些是您的实际主题ID吗?我认为这是对数据的简化。如果是,那么下面的答案将起作用,您应该给出反馈,并通过单击您使用的答案旁边的复选标记来标记其中一个答案是否正确,否则,您将无法使用公式执行此操作,并且需要在第三列中放置值a或vba来执行此操作。ID和值a列的内容是简化的:ID和其他字段中的值(在我的实际数据集中是数字)之间没有固有的关系。很抱歉造成混淆,但迄今为止提供的答案没有一个真正回答我的问题,因为它们假定了一种不存在的内在关系。那么,您是在寻找vba的适当位置,还是使用第三列公式?我也可以。对于公式;对索引/匹配进行一些研究