扩大了我之前提出的一个问题,但是现在由于你们精力充沛的人和其他来源的帮助,我现在更进一步。
基本上我可以从公司服务器提取报告,它们以电子表格格式导出,每行都是一个报告,每列都是关于报告的信息(报告计数,日期,报告标题等),第I列我关注的是一个4位数代码,用于识别报告来自的组(A205,A206,B208,Q404,有数千个)让我们将此列称为"报告编号"
我目前正在使用VLOOKUP在参考表上查找代码,然后返回与代码相关联的组的名称,因此如果代码是" A205",则公式将返回" A-TEAM"在牢房里。 (我把它与一个宏配对,一次完成数百行,并在下一个空列填写名称)
现在这很有效......如果"报告编号"中只有一个代码。柱。当报告由多个以逗号分隔的组完成时,我的问题就出现了。所以在"报告编号"专栏,它可能有" A205,A206,B208"我需要公式以相同的格式输出所有解码的名称(I.E." A Team,B Team,C Team)而不是错误,或者只是第一个。
那么,有没有办法用VLOOKUP做到这一点?没有反复嵌套IF功能。或者我需要修改我的宏?
这是我当前的宏工作(当我更改参数以匹配我的工作表名称和诸如此类别时),您可以看到输入vlookup公式的位置。
Option Explicit
Sub CustomerCodeLookup()
Dim LastRow As Long
Dim LastColumn As Long
Dim RNColumn As Long
Dim RNFirstCell As String
'identify last row of data
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'get first blank column (by assuming first blank cell in row 1 is the first blank column)
LastColumn = Cells(1, 1).End(xlToRight).Column + 1
'find the column that has "Report Number"
RNColumn = Range("1:1").Find("ReportNumber", LookIn:=xlValues).Column
'Relative address of first cell in Report Number column for use in the formula
RNFirstCell = Cells(2, RNColumn).Address(False, False)
'Add header to the lookup column
Cells(1, LastColumn) = "Group Name"
'insert formula from row 2 until the last data row
Range(Cells(2, LastColumn), Cells(LastRow, LastColumn)) = "=VLOOKUP(LEFT(" & RNFirstCell & ", 5),'C:\Path\to\pulled workbook\[Codes.xlsm]Codereference'!$A:$O,2,0)"
'Remove formula from cells
Range(Cells(2, LastColumn), Cells(LastRow, LastColumn)) = Range(Cells(2, LastColumn), Cells(LastRow, LastColumn)).Value
End Sub
答案 0 :(得分:1)
你真的不想使用VLOOKUP来解决这个问题。根据您拥有的Excel版本,以下任何一种方法都是更好的方法
请注意,如果您在2010年和我上面提到的日期之间运行版本,则PowerQuery和PowerPivot都可以作为免费插件使用。
PowerQuery和PowerPivot是解决您问题的最简单方法,网上有大量资源和YouTube上的视频可以帮助您入门。
答案 1 :(得分:0)
因为你实际上并不需要公式并且尝试使用jeffreyweir数组/字典建议:
Sub CustomerCodeLookup()
Dim P1 As Range, P2 As Range
Dim T2()
Set D1 = CreateObject("scripting.dictionary")
Set P1 = ActiveSheet.UsedRange
Set P2 = Workbooks("Codes.xlsm").Sheets("Codereference").UsedRange
T1 = P1
T3 = P2
For i = 1 To UBound(T3): D1(T3(i, 1)) = T3(i, 2): Next i
For i = 1 To UBound(T1, 2)
If T1(1, i) Like "ReportNumber" Then RN = i
Next i
a = 1
For i = 2 To UBound(T1)
ReDim Preserve T2(1 To a)
St1 = Split(Trim(T1(i, RN)), ",")
For j = 0 To UBound(St1)
T2(a) = T2(a) & ", " & D1(St1(j))
Next j
T2(a) = Mid(T2(a), 3)
a = a + 1
Next i
Range("A1").End(xlToRight).Offset(1, 1).Resize(a - 1) = Application.Transpose(T2)
End Sub
编辑:
Sub CustomerCodeLookup()
Dim P1 As Range, P2 As Range
Dim T2()
Set D1 = CreateObject("scripting.dictionary")
Set P1 = ActiveSheet.UsedRange
Set P2 = Workbooks("Codes.xlsm").Sheets("Codereference").UsedRange
T1 = P1
T3 = P2
'Line below feeds the dictionary as D1(Key)=Item where Key (T3(i, 1)) is first used column of Workbooks("Codes.xlsm").Sheets("Codereference") and Item (T3(i, 2)) second column
For i = 1 To UBound(T3): D1(T3(i, 1)) = T3(i, 2): Next i
For i = 1 To UBound(T1, 2)
If T1(1, i) Like "ReportNumber" Then RN = i
Next i
a = 1
For i = 2 To UBound(T1)
ReDim Preserve T2(1 To a)
St1 = Split(Trim(T1(i, RN)), ",")
For j = 0 To UBound(St1)
T2(a) = T2(a) & ", " & D1(Left(Trim(St1(j)), 5))
Next j
T2(a) = Mid(T2(a), 3)
a = a + 1
Next i
Range("A1").End(xlToRight).Offset(1, 1).Resize(a - 1) = Application.Transpose(T2)
Range("A1").End(xlToRight).Offset(0, 1) = "Group Name"
End Sub