使用Excel VBA查找,转换和替换字符串中的货币

时间:2014-08-03 09:10:42

标签: excel vba excel-vba

我有一个像这样的字符串的单元格

(H/Y): As per un-audited half yearly accounts as on 30.06.2014 (Jan'14 to June'14), the Company has reported consolidated net profit after tax (excluding non-controlling interests) of **Tk. 5.87** million with consolidated EPS of **Tk. 0.05** as against **Tk. 33.46** million and **Tk. 0.31** respectively for the same period of the previous year. Whereas consolidated net profit after tax (excluding non-controlling interests) was **Tk. 2.71** million with consolidated EPS of **Tk. 0.02** for the period of 3 months (Apr'14 to June'14) ended on 30.06.2014 as against **Tk. 7.42** million and **Tk. 0.07** respectively for the same period of the previous year.

我想查找,货币转换并替换所有 Tk。 5.87 Tk。 0.05

我已经可以使用以下excel公式执行此操作。

=REPLACE(A1,FIND(" Tk. ",A1,1)+5,FIND(" ",A1,FIND(" Tk. ",A1,1)+5)-(FIND(" Tk. ",A1,1)+5),(MID(A1,FIND(" Tk. ",A1,1)+5, FIND(" ", A1,FIND(" Tk. ",A1,1)+5)-(FIND(" Tk. ",A1,1)+5)))/B1)

=REPLACE(C1,FIND(" Tk. ",A1,1), FIND(" ", A1,FIND(" Tk. ",A1,1)+5)-(FIND(" Tk. ",A1,1)+5)," USD")

,结果是(注意只转换第一个)

(H/Y): As per un-audited half yearly accounts as on 30.06.2014 (Jan'14 to June'14), the Company has reported consolidated net profit after tax (excluding non-controlling interests) of USD 0.073375 million with consolidated EPS of Tk. 0.05 as against Tk. 33.46 million and Tk. 0.31 respectively for the same period of the previous year. Whereas consolidated net profit after tax (excluding non-controlling interests) was Tk. 2.71 million with consolidated EPS of Tk. 0.02 for the period of 3 months (Apr'14 to June'14) ended on 30.06.2014 as against Tk. 7.42 million and Tk. 0.07 respectively for the same period of the previous year.

但是我不想使用Excel公式,而是想在VBA转换工作簿表中的所有货币时执行此操作。

这就是我到目前为止所做的事情

Sub Curr_Convert()

Dim count As Single
Dim No_of_Char As Double
Dim Val_2_Cnvrt As Double
Dim Val_aftr_Cnvrt As Double
Dim Uncnvrtd_Strng As String
Dim cnvrtd_Strng As String
Dim cnvrtd_Strng2 As String
Dim Cnvrtin_Rate As Single

Cnvrtin_Rate = 80
count = 0



Uncnvrtd_Strng = Range("A1")

Do While count < 10


No_of_Char = (InStr((InStr(1, Uncnvrtd_Strng, "Tk. ") + 4), Uncnvrtd_Strng, " ")) - (InStr(1, Uncnvrtd_Strng, "Tk. ") + 4)

Val_2_Cnvrt = Mid(Uncnvrtd_Strng, (InStr(1, Uncnvrtd_Strng, "Tk. ") + 4), No_of_Char)

Val_aftr_Cnvrt = Val_2_Cnvrt / Cnvrtin_Rate

Range("A7").Value = Val_aftr_Cnvrt

cnvrtd_Strng = Replace(Range("A1"), Val_2_Cnvrt, Val_aftr_Cnvrt)

cnvrtd_Strng2 = Replace(cnvrtd_Strng, "Tk. ", "USD ", 1, 1)
Uncnvrtd_Strng = cnvrtd_Strng2


count = count + 1

Loop
Range("A8").Value = cnvrtd_Strng2


End Sub

感谢任何帮助。提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以使用regular expressions来匹配字符串中的模式。在这种情况下,&#34;模式&#34;是:

"Tk\. \d*\.\d{2}"

这意味着我们正在尝试查找以&#34; Tk开头的所有子字符串。&#34;后跟一个空格,以及带小数点后2位的数字小数。这可以修改,但目前将匹配任何长度数,无论是0.02还是12345.67等。

需要引用Microsoft VBScript Regular Expressions 5.5

假设转换率/乘数在范围内(&#34; B1&#34;) - 对于我的测试,我使用了值0.0129155 from XE.com

Sub fixcurrency()
    Dim numValue As String
    Dim replacement As String
    Dim fullReplacement As String
    Dim cl As Range
    Dim rng As Range
    Dim conversionRate As Double
    Dim regexp As regexp
    Dim allMatches As MatchCollection
    Dim m As Match

    'This will loop all cells in range A1:A10
    ' Modify the next line to use a different range
    Set rng = Range("A1:A10")

    'Assumes the conversion rate in range("B1")
    conversionRate = Range("B1")


    'Create our RegExp engine
    Set regexp = New regexp

    regexp.Global = True
    regexp.IgnoreCase = False
    regexp.Pattern = "Tk\. \d*\.\d{2}"

    For Each cl In rng.Cells
        fullReplacement = cl.Value

        Set allMatches = regexp.Execute(cl.Value)
        For Each m In allMatches

            'Remove the "tk. " from the cell's value & trim any leading/trailing spaces
            numValue = Trim(Replace(m.Value, "Tk.", " "))

            'Display the numeric value to ensure it is working correctly.
            ' once confirmed, remove this line:
            'MsgBox numValue

            'Compute the new value: multiply the numValue by the conversion rate, round to 2 decimals.
            replacement = "USD " & Round((CDbl(numValue) * conversionRate), 4)

            fullReplacement = Replace(fullReplacement, m.Value, replacement)
        Next
        'replace the cell's text with the replacement text
        cl.Value = fullReplacement
    Next
End Sub

使用您的示例数据和孟加拉国塔卡兑换美元的转换率(截至今天上午的当前时间:http://www.xe.com/currencyconverter/convert/?From=BDT&To=USD),输出如下:

  

根据截至2014年6月30日(1月及14日至6月及14月14日)未经审核的半年度账目,本公司已报告的综合税后净利润(不包括非控股权益)合并每股盈利0.0758美元 USD 0.0006 ,与之前同期相比, USD 0.4322 百万和 USD 0.004 年。鉴于税后综合净利润(不包括非控股权益) 0.035 百万,合并每股盈利 0.0003 为3个月(4月14日至14日) 6月&14; 14)于2014年6月30日结束,与去年同期的 0.0958 0.0009 分别相比。

  

本公司已报告综合除税后利润(不包括非控股权益)为560.43万美元,合并每股收益为0.0107美元

答案 1 :(得分:0)

这已经放弃了我需要处理的一些错误,但它或多或少地做了我需要做的事情

Sub Curr_Convert()

Dim Cnvrtin_Rate As Single
Dim No_of_Char As Double
Dim Val_2_Cnvrt As Double
Dim Val_aftr_Cnvrt As Double

Dim Uncnvrtd_Strng As String
Dim cnvrtd_Strng As String
Dim cnvrtd_Strng2 As String

Dim cl As Range
Dim rng As Range

Cnvrtin_Rate = 80
'This will loop all cells in range A1:A10
' Modify the next line to use a different range
Set rng = Range("A1:A300")


For Each cl In rng.Cells
If Not cl.Value = vbNullString And Not IsError(cl.Value) Then

'String that need to be converted
Uncnvrtd_Strng = cl.Value

No_of_Char = (InStr((InStr(1, Uncnvrtd_Strng, "Tk. ") + 4), Uncnvrtd_Strng, " ")) - (InStr(1, Uncnvrtd_Strng, "Tk. ") + 4)

' Extracting the Currency that will be converted from the string
Val_2_Cnvrt = Mid(Uncnvrtd_Strng, (InStr(1, Uncnvrtd_Strng, "Tk. ") + 4), No_of_Char)

'Value of the converted Currency
Val_aftr_Cnvrt = Val_2_Cnvrt / Cnvrtin_Rate

'Replacing the Currency Value in the old string
cnvrtd_Strng = Replace(Uncnvrtd_Strng, Val_2_Cnvrt, Val_aftr_Cnvrt)

'Replacing the Currecy symbol
cnvrtd_Strng2 = Replace(cnvrtd_Strng, "Tk. ", "USD ", 1, 1)

'
cl.Value = cnvrtd_Strng2

End If

Next
End Sub

示例是

单元格A1包含以下字符串

公司已报告Tk的税后综合利润(不包括非控股权益)。合并每股收益为436.7百万英镑。 0.83

运行我的宏后,这就是我在Cell A1中得到的

公司已报告合并后的综合每股收益为Tk,其中税后综合利润(不包括非控制性权益)为54588.75万美元。 0.83

现在我遇到了一些事情,1是我无法让它运行直到所有货币都转换为美元。

感谢大卫提供的一些指示,但是对此有任何帮助表示赞赏