根据Vlookup替换到列表

时间:2019-03-14 15:22:59

标签: excel vba replace vlookup

我有一个80字节的文本文件,我正在从中提取数据。我需要将单元格(j,5)= Mid(tempstr,43,13)的结果转换为美元数字。

中间结果: 000000148012H

最右边的字符是“ H”,需要根据下表转换为“ 8”

右字符

B   2   1
C   3   1
D   4   1
E   5   1
F   6   1
G   7   1
H   8   1
I   9   1
{   0   1
J   1   -1
K   2   -1
L   3   -1
M   4   -1
N   5   -1
O   6   -1
P   7   -1
Q   8   -1
R   9   -1
}   0   -1

在调用宏之后,excel公式在工作表中作为单独的单元格工作,但是我需要在工作表(j,5)和Cells(j,6)中的此工作表的VBA代码中使用它。 E2 =中间结果(000000148012H),数据表=上表

Excel公式:

=REPLACE(E2,13,1,VLOOKUP(RIGHT(E2,1),'Data 
Sheet'!$A$2:$C$21,2,FALSE))*VLOOKUP(RIGHT(E2,1),'Data 
Sheet'!$A$2:$C$21,3,FALSE)*0.01

Reslut = 14801.28

当前VBA代码:

If Val(Mid(tempstr, 13, 12)) <> 0 And Val(Mid(tempstr, 25, 7)) = "0000077" Then
    Worksheets(2).Activate
    If j = 1 Then
        Cells(j, 1) = "SSN"
        Cells(j, 2) = "Transaction"
        Cells(j, 3) = "Source"
        Cells(j, 4) = "Fund Symbol"
        Cells(j, 5) = "Cash/Market Value"
        Cells(j, 6) = "Shares"
        Cells(j, 7) = "Cost Converted"
        Cells(j, 8) = "C/M/V Converted"
        Cells(j, 9) = "Shares Converted"
        j = j + 1
    End If
        Cells(j, 1) = Mid(tempstr, 13, 12)  'SSN
        Cells(j, 2) = Mid(tempstr, 32, 3)   'Transaction
        Cells(j, 3) = Mid(tempstr, 35, 2)   'Source
        Cells(j, 4) = Mid(tempstr, 39, 4)   'Fund Symbol
        Cells(j, 5) = Mid(tempstr, 43, 13)  'Cash/Market Value
        Cells(j, 6) = Mid(tempstr, 56, 12)  'Shares
        Cells(j, 7) = Mid(tempstr, 68, 13)  'Cost
        j = j + 1
End If

1 个答案:

答案 0 :(得分:0)

我没有收到我的问题的任何答案,因此我仅对值进行了硬编码,如下所示。似乎效果很好,但是如果文件中有任何更改,将需要大量更新。

   If Val(Mid(tempstr, 13, 12)) <> 0 And Val(Mid(tempstr, 25, 7)) = "0000077" Then
    Worksheets(2).Activate
    If j = 1 Then
        Cells(j, 1) = "SSN"
        Cells(j, 2) = "Transaction"
        Cells(j, 3) = "Source"
        Cells(j, 4) = "Fund Symbol"
        Cells(j, 5) = "Cash/Market Value"
        Cells(j, 6) = "Shares"
        Cells(j, 7) = "Cost"
        j = j + 1
        End If
        Cells(j, 1) = Mid(tempstr, 13, 12)                                                 'SSN
        Cells(j, 2) = Mid(tempstr, 32, 3)                                                  'Transaction
        Cells(j, 3) = Mid(tempstr, 35, 2)                                                  'Source
        Cells(j, 4) = Mid(tempstr, 39, 4)                                                   'Fund Symbol
        If Mid(tempstr, 55, 1) = "A" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "1") * 0.01
        If Mid(tempstr, 55, 1) = "B" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "2") * 0.01
        If Mid(tempstr, 55, 1) = "C" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "3") * 0.01
        If Mid(tempstr, 55, 1) = "D" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "4") * 0.01
        If Mid(tempstr, 55, 1) = "E" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "5") * 0.01
        If Mid(tempstr, 55, 1) = "F" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "6") * 0.01
        If Mid(tempstr, 55, 1) = "G" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "7") * 0.01
        If Mid(tempstr, 55, 1) = "H" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "8") * 0.01
        If Mid(tempstr, 55, 1) = "I" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "9") * 0.01
        If Mid(tempstr, 55, 1) = "{" Then Cells(j, 5) = 1 * (Mid(tempstr, 43, 12) & "0") * 0.01
        If Mid(tempstr, 55, 1) = "J" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "1") * 0.01
        If Mid(tempstr, 55, 1) = "K" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "2") * 0.01
        If Mid(tempstr, 55, 1) = "L" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "3") * 0.01
        If Mid(tempstr, 55, 1) = "M" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "4") * 0.01
        If Mid(tempstr, 55, 1) = "N" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "5") * 0.01
        If Mid(tempstr, 55, 1) = "O" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "6") * 0.01
        If Mid(tempstr, 55, 1) = "P" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "7") * 0.01
        If Mid(tempstr, 55, 1) = "Q" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "8") * 0.01
        If Mid(tempstr, 55, 1) = "R" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "9") * 0.01
        If Mid(tempstr, 55, 1) = "}" Then Cells(j, 5) = -1 * (Mid(tempstr, 43, 12) & "0") * 0.01           'Cash/Market Value
        If Mid(tempstr, 67, 1) = "A" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "1") * 0.01
        If Mid(tempstr, 67, 1) = "B" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "2") * 0.01
        If Mid(tempstr, 67, 1) = "C" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "3") * 0.01
        If Mid(tempstr, 67, 1) = "D" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "4") * 0.01
        If Mid(tempstr, 67, 1) = "E" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "5") * 0.01
        If Mid(tempstr, 67, 1) = "F" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "6") * 0.01
        If Mid(tempstr, 67, 1) = "G" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "7") * 0.01
        If Mid(tempstr, 67, 1) = "H" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "8") * 0.01
        If Mid(tempstr, 67, 1) = "I" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "9") * 0.01
        If Mid(tempstr, 67, 1) = "{" Then Cells(j, 6) = 1 * (Mid(tempstr, 56, 11) & "0") * 0.01
        If Mid(tempstr, 67, 1) = "J" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "1") * 0.01
        If Mid(tempstr, 67, 1) = "K" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "2") * 0.01
        If Mid(tempstr, 67, 1) = "L" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "3") * 0.01
        If Mid(tempstr, 67, 1) = "M" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "4") * 0.01
        If Mid(tempstr, 67, 1) = "N" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "5") * 0.01
        If Mid(tempstr, 67, 1) = "O" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "6") * 0.01
        If Mid(tempstr, 67, 1) = "P" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "7") * 0.01
        If Mid(tempstr, 67, 1) = "Q" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "8") * 0.01
        If Mid(tempstr, 67, 1) = "R" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "9") * 0.01
        If Mid(tempstr, 67, 1) = "}" Then Cells(j, 6) = -1 * (Mid(tempstr, 56, 11) & "0") * 0.01          'Shares
        If Mid(tempstr, 80, 1) = "A" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "1") * 0.01
        If Mid(tempstr, 80, 1) = "B" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "2") * 0.01
        If Mid(tempstr, 80, 1) = "C" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "3") * 0.01
        If Mid(tempstr, 80, 1) = "D" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "4") * 0.01
        If Mid(tempstr, 80, 1) = "E" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "5") * 0.01
        If Mid(tempstr, 80, 1) = "F" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "6") * 0.01
        If Mid(tempstr, 80, 1) = "G" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "7") * 0.01
        If Mid(tempstr, 80, 1) = "H" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "8") * 0.01
        If Mid(tempstr, 80, 1) = "I" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "9") * 0.01
        If Mid(tempstr, 80, 1) = "{" Then Cells(j, 7) = 1 * (Mid(tempstr, 68, 12) & "0") * 0.01
        If Mid(tempstr, 80, 1) = "J" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "1") * 0.01
        If Mid(tempstr, 80, 1) = "K" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "2") * 0.01
        If Mid(tempstr, 80, 1) = "L" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "3") * 0.01
        If Mid(tempstr, 80, 1) = "M" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "4") * 0.01
        If Mid(tempstr, 80, 1) = "N" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "5") * 0.01
        If Mid(tempstr, 80, 1) = "O" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "6") * 0.01
        If Mid(tempstr, 80, 1) = "P" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "7") * 0.01
        If Mid(tempstr, 80, 1) = "Q" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "8") * 0.01
        If Mid(tempstr, 80, 1) = "R" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "9") * 0.01
        If Mid(tempstr, 80, 1) = "}" Then Cells(j, 7) = -1 * (Mid(tempstr, 68, 12) & "0") * 0.01               'Cost
        j = j + 1
        End If