转换为字符串在十进制之后删除0' s

时间:2017-07-24 01:33:14

标签: excel vba excel-vba excel-2013

我正在使用下面的Implode()方法将数据从源工作簿复制到目标工作簿。我遇到的问题是在源工作簿中格式为7.00但在目标工作簿中格式为7,我相信这是由于CStr(MyR(1, i)),即ConvertToString。如何更改此方法,以便如果列为numeric格式,一旦将其复制到目标工作簿,它将再次采用numeric格式?

Private Function Implode(ByVal R As Range, Optional ByVal D As String = strSeparator) As String
    Dim i As Long, ii As Long, str As String, MyR() As Variant
    MyR = R
    For i = 1 To R.Columns.Count
        isPercent = False
        If iPC > 0 And IsNumeric(MyR(1, i)) And MyR(1, i) <> "" Then
            For ii = 1 To iPC
                If i = PercCols(ii) Then
                    isPercent = True
                    Exit For
                End If
            Next ii
        End If
        str = CStr(MyR(1, i))
        If InStr(1, str, D) > 0 Then str = """" & str & """"
        If i = 1 Then
            Implode = str
        Else
            Implode = Implode & D & str
        End If
    Next i
End Function

1 个答案:

答案 0 :(得分:2)

可以尝试

 If IsNumeric(MyR(1, i)) Then 'Check for numeric
      Round(CDec(MyR(1, i)),2)
 Else
      CStr(MyR(1, i))
 End If

CDec允许没有小数的人显示为整数

More Info on Conversion Function