如何在访问vba中的字符串之间添加任何字符

时间:2017-01-09 14:11:19

标签: vba access-vba access

我喜欢" NIFTY29-12-2016CE6300.00" 我希望输出为: " NIFTY_29-12-2016_6300_CE"

问题是第一部分,即(NIFTY)长度不固定它可以是abcd,rdftghe或任何东西 最后一部分即(6300.00)也不固定长度可以是123.8888888.23.88989或其他任何东西

尝试使用此代码获取字符串中第一个数字的位置,然后我就可以连续#34; _"在此之前代码如下:

  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

我一直在努力:" NIFTY _" 请帮我!!!!提前谢谢

1 个答案:

答案 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