2017-01-09 26 views
0

我有串像「NIFTY29-12-2016CE6300.00」 ,我想作爲輸出: 「NIFTY_29-12-2016_6300_CE」如何串之間的訪問VBA添加任何字符

問題是第一部分即(NIFTY)是不固定的長度也可以是ABCD,rdftghe或任何 而最後部分,即(6300.00)也不是固定的長度也可以是123.8888888.23.88989或任何

試試這個代碼來獲得的位置字符串中的第一個數字,我能夠在之前連接「_」,代碼如下:

If InStr(CStr(rs.Fields!Symbol), "CE") Then 
        StrOg = CStr(rs.Fields!Symbol) 

        For i = 1 To Len(StrOg) 

         currentCharacter = Mid(StrOg, i, 1) 
         If IsNumeric(currentCharacter) = True Then 
          GetPosChar = i 
          Exit For 
         End If 
        Next i 
        strtemp = Left(StrOg, GetPosChar) & "_" & Right() & "_" 
       Else 

我正在acheving直到這個:「NIFTY_」 請幫我!!!!在此先感謝

+0

在你的問題中有2個替代品。一個是第一個整數,第二個是最後一個小數。我對麼 ?你在第二次更換時解決了第一次更換? –

+0

我仍然無法提取日期和字符串末尾的數字,並重新排列字符串的預期結果... –

回答

1

請嘗試以下。既然你沒有給出適當的解釋,哪裏需要改變。我用一些假設編寫了代碼,符號CE可供您使用,我們需要截斷所有小數部分,等等。您可以看到代碼並繼續下一步。

Private Sub test() 
    Dim StrOg As String 

    StrOg = "NIFTY29-12-2016CE6123.8888888" 
    Debug.Print "Org=" & StrOg 
    Debug.Print "New=" & ReFormat(StrOg) 

End Sub 

Private Function ReFormat(ByVal inputText) As String 
    Dim strNew As String 
    Dim charPos As Integer 
    Dim counter As Integer 
    Dim found As Boolean 

    'Search for 1st character from reverse and remove the 2 charters (i.e. symbol CE) 
    found = False 
    For i = Len(inputText) To 1 Step -1 
    If (Not IsNumeric(Mid$(inputText, i, 1))) And Mid$(inputText, i, 1) <> "." Then 
     charPos = i 
     found = True 
     Exit For 
    End If 
    Next i 
    If found Then 
    strNew = Left$(inputText, charPos - 2) & "_" & Right$(inputText, Len(inputText) - charPos) 
    Else 
    strNew = inputText 
    End If 

    'Search for 1st digit and if found update the string 
    found = False 
    For i = 1 To Len(strNew) 
    If IsNumeric(Mid$(strNew, i, 1)) Then 
     charPos = i 
     found = True 
     Exit For 
    End If 
    Next i 
    If found Then 
    strNew = Left$(strNew, charPos - 1) & "_" & Right$(strNew, Len(strNew) - charPos + 1) 
    End If 

    'Search for last decimal and truncate thereAfter 
    charPos = InStrRev(strNew, ".") 
    If charPos > 0 Then 
    strNew = Left$(strNew, charPos - 1) & "_CE" 
    End If 

    ReFormat = strNew 

End Function 
+0

感謝@Mukul Varshney它的作品!!!!! –

相關問題