0

我必须在字段中搜索一些字符串并执行计算。

比如我有这个字段100KSGD,

我必须得到 K,然后将 100 的值乘以 1000,所以我会得到 100,000SGD。

接下来是,我必须得到 SGD 并乘以 SGD 对 USD 的汇率。这就是我必须得到的价值。

我使用 Instr 来获取 K,如果字段中没有其他字符串,我能够执行计算。

这是我的代码:

    If InStr(1 , OEMTEST_String$ , HasString_K$, 5) > 0 Then
            Print "Searching for letter K in OEMTEST field..."
            MsgBox("Letter K is found in OEMTEST field!")
            Print "Converting OEMTEST field..."

        'Replace  All occurrences of K  
        tempPosition = InStr( 1, OEMTEST_String$, HasString_K$ )    
        If( ( Len( OEMTEST_String$ ) - Len( HasString_K$ ) ) + 1 = tempPosition ) Then 
            OEMnvar = Left( OEMTEST_String$, tempPosition - 1 )
            MsgBox "The number value in OEMTEST field is " & OEMnvar

            OEMnewvar = CDbl(OEMnvar) 'change nvar from string to double
            OEMupdatedVar = CDbl(OEMnewvar * 1000) 'multiply the value by 1000

            MsgBox "new value of OEMTEST is " & OEMupdatedvar 'check message box

            'replace the value and save the document
            Print "Saving converted value of OEMTEST field..."
            Call note.ReplaceItemValue("OEMTEST", OEMupdatedVar)
            Call note.Save(True, False) 

        End If
        Else
        Print "Letter K not found in OEMTEST field..."
    End If

我怎样才能达到我需要的价值?我如何做一个嵌套的 Instr?

非常感谢!

4

2 回答 2

0
' havent tried in script this but how about something like this :


const K="K"
dim sStartVal as string
dim sMoneyBit as string
dim sCurrencyBit as string
dim dMoney as double
dim sNewMoney as double
dim sngRatio as single

sngRato = 0.666 ' whatever the ratio of SGD to USD is
sStartVal="100KSGD"
if instr(sStartVal,"K")>0 then
    ' we have a K - might be at the beginning though
     sMoneyBit=strleft(sStartVal, K) ' stleft and strright get the string to left of right of second param
     sCurrencyBit=strright(sStartVal,K) ' never used, but your logic should use this to get the right ratio ?
     dMoney = cdbl("0"+sMoneyBit) * 1000 ' here cos we found a K so multiply out. Leading extra zero just handles case where string starts with a K
     dNewMoney = dMoney * sngRatio
end if
' dNewMoney = answer, or zero if no K found
于 2013-09-23T16:56:30.930 回答
0

这有很多漏洞,但主要技术是调用Split( InputValue, "K")以获取数字和转换符号。

Option Public
Option Declare

Sub Initialize
'
' We make lots of mistakes, here. Among the more serious:
'
'   1. Assume 'K' is always present between the number and the conversion symbol.
'   2. Assume the conversion symbol never has 'K' in it.
'   3. No error checking
'   4. Hard-coded conversion rate.
'
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

    Const K_SEP = "K"

    Dim strParseThis As String
    Dim varParts As Variant
    Dim dblValue As Double
    Dim dblRate List As Double

    ' Build your conversion table. (Here as a List or load from a database.)    
    dblRate("SGD") = 0.7500 ' made up value, of course.

    strParseThis = "100KSGD"

    varParts = Split( strParseThis, K_SEP )

'   Print {"} + Join( varParts, {", "} ) + {"}

    If UBound( varParts ) = 1 Then
        ' Now we have an array with "100" in the first element and "SGD" in the second
        dblValue = Cdbl( varParts(0) ) * 1000.00 * dblRate( varParts(1) )
        Print strParseThis + " converts to " + Format( dblValue, "#,##0.00" ) + " USD"

    Else
        Print strParseThis + " did not have a " + K_SEP

    End If

End Sub
于 2013-10-08T16:19:46.157 回答