使用脚本字典查找值,然后打印键
using scripting dictionary to find values, then print key
所以在问了几个问题后,在其他人的帮助下,我现在已经有了下面的 VBA 宏脚本。它引用了客户代码的主列表及其代表的组别 (worksheet "CustomerCodeReference")。它应该比较提取报告中的值(在标记为 "ReportNumber" 的列下)并找到该列中列出的客户代码,以及 return 下一个可用空列中的解码名称。
到目前为止,如果 "ReportNumber" 列包含报告编号:
"A20312345678901, A20212345678901" 它应该比较客户代码(数字的前 4 个字符,在上述情况下它们是 "A203, A202")在 "CustomerCodeReference" sheet,然后 return 他们在空列中引用的组名(在本例中 "B Team, A Team")
但是,目前的问题是,如果有多个值,它只是 returning 逗号,如果单元格中只有一个报告编号,则什么也没有。
(因此 "A20312345678901, A20212345678901" 将 return 在空单元格中添加一个“,”)
好像很接近,因为如果有3个值,就会return2个逗号,但是没有名字。
有任何想法吗?
Sub CustomerCodeLookup()
'sets the sheet I'm searching (P1), The sheet where the list of codes and their group names are (P2) and creates the dictionary
Dim P1 As Range, P2 As Range
Dim T2()
Set D1 = CreateObject("scripting.dictionary")
Set P1 = ActiveSheet.UsedRange
Set P2 = Workbooks("ReportsMac.xlsm").Sheets("CustomerCodeReference").UsedRange
T1 = P1
T3 = P2
'Finds the number of cells with data in reference sheets, in case it changes
For i = 1 To UBound(T3): D1(T3(i, 1)) = T3(i, 2): Next i
'finds ReportNumber Column
For i = 1 To UBound(T1, 2)
If T1(1, i) Like "ReportNumber" Then RN = i
Next i
'Here is where problem may be, supposed to identify codes in the column, separate them by comma, and set them aside to be transposed into empty cell.
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
'add the results to empty cell
Range("A1").End(xlToRight).Offset(1, 1).Resize(a - 1) = Application.Transpose(T2)
End Sub
你能试试看这是否能解决问题吗?
For i = 2 To UBound(T1)
ReDim Preserve t2(1 To a)
St1 = Split(Trim(T1(i, RN)), ",")
For j = 0 To UBound(St1)
If t2(a) = "" Then
t2(a) = D1(St1(j))
Else
t2(a) = t2(a) & ", " & D1(St1(j))
End If
Next j
a = a + 1
Next i
所以在问了几个问题后,在其他人的帮助下,我现在已经有了下面的 VBA 宏脚本。它引用了客户代码的主列表及其代表的组别 (worksheet "CustomerCodeReference")。它应该比较提取报告中的值(在标记为 "ReportNumber" 的列下)并找到该列中列出的客户代码,以及 return 下一个可用空列中的解码名称。
到目前为止,如果 "ReportNumber" 列包含报告编号:
"A20312345678901, A20212345678901" 它应该比较客户代码(数字的前 4 个字符,在上述情况下它们是 "A203, A202")在 "CustomerCodeReference" sheet,然后 return 他们在空列中引用的组名(在本例中 "B Team, A Team")
但是,目前的问题是,如果有多个值,它只是 returning 逗号,如果单元格中只有一个报告编号,则什么也没有。 (因此 "A20312345678901, A20212345678901" 将 return 在空单元格中添加一个“,”)
好像很接近,因为如果有3个值,就会return2个逗号,但是没有名字。 有任何想法吗?
Sub CustomerCodeLookup()
'sets the sheet I'm searching (P1), The sheet where the list of codes and their group names are (P2) and creates the dictionary
Dim P1 As Range, P2 As Range
Dim T2()
Set D1 = CreateObject("scripting.dictionary")
Set P1 = ActiveSheet.UsedRange
Set P2 = Workbooks("ReportsMac.xlsm").Sheets("CustomerCodeReference").UsedRange
T1 = P1
T3 = P2
'Finds the number of cells with data in reference sheets, in case it changes
For i = 1 To UBound(T3): D1(T3(i, 1)) = T3(i, 2): Next i
'finds ReportNumber Column
For i = 1 To UBound(T1, 2)
If T1(1, i) Like "ReportNumber" Then RN = i
Next i
'Here is where problem may be, supposed to identify codes in the column, separate them by comma, and set them aside to be transposed into empty cell.
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
'add the results to empty cell
Range("A1").End(xlToRight).Offset(1, 1).Resize(a - 1) = Application.Transpose(T2)
End Sub
你能试试看这是否能解决问题吗?
For i = 2 To UBound(T1)
ReDim Preserve t2(1 To a)
St1 = Split(Trim(T1(i, RN)), ",")
For j = 0 To UBound(St1)
If t2(a) = "" Then
t2(a) = D1(St1(j))
Else
t2(a) = t2(a) & ", " & D1(St1(j))
End If
Next j
a = a + 1
Next i