遇到了一个小问题,但我做不到。也许有人可以帮助我。非常感兴趣。
我的小数分隔符的区域设置是“。” (DOT)。
我有一个excel文件,其中包含一些列中的小数位。在更新excel模板中的数据时,我关闭了用户系统设置,然后应用DOT作为分隔符。将其转换为文本文件时,它将逗号“,”显示为小数点分隔符。
我正在使用以下代码
申请书 StrDecimal = .DecimalSeparator
StrThousand = .ThousandsSeparator
.DecimalSeparator = "."
.ThousandsSeparator = "'"
.UseSystemSeparators = False
End With
需要检查十进制分隔符是否不是“。”那么我们需要强制使用“。” aS小数分隔符。
请使用VBA或使用Datavalidation来帮助您实现此目的 非常感谢你提前!
答案 0 :(得分:1)
因此,您已获得数据并将其写入文本文件。假设你的数据在一个数组中,我会循环遍历数组并在将数据转换为字符串之后转换数据,就像这样。
Sub ChangeDecimalSeperator()
Dim vData As Variant, ii As Integer, jj As Integer
vData = ActiveSheet.Range("A1:B2")
For ii = LBound(vData, 1) To UBound(vData, 1)
For jj = LBound(vData) To UBound(vData, 2)
'Only convert if it's a number
If VBA.IsNumeric(vData(ii, jj)) Then
'You can also specify the format using VBA.Format$() here, ie force the decimal places
vData(ii, jj) = ReplaceCharacter(VBA.CStr(vData(ii, jj)), ".", ",")
End If
Next jj
Next ii
'Now output vData to your textfile
End Sub
Function ReplaceCharacter(sForString As String, sReplacingCharacter As String, sWithCharacter As String) As String
Dim sStringPrevious As String
'While there is a character that we still need replacing left, keep going. If we can't find one, we're done
Do
sStringPrevious = sForString
sForString = VBA.Replace(sForString, sReplacingCharacter, sWithCharacter)
Loop Until sStringPrevious = sForString
ReplaceCharacter = sForString
End Function