首页 文章

如何将点固定为excel中的小数分隔符

提问于
浏览
0

遇到了一个小问题,但我无法做到 . 也许有人可以帮助我 . 非常感兴趣 .
我的十进制分隔符的区域设置是"."(点) .
我有一个excel文件,其中包含一些列中的小数位 . 在更新excel模板中的数据时,我关闭了用户系统设置,然后应用DOT作为分隔符 . 将其转换为文本文件时,它将逗号","显示为小数点分隔符 . 我正在使用下面的代码来转动用户设置

使用Application StrDecimal = .DecimalSeparator StrThousand = .ThousandsSeparator

.DecimalSeparator = "."  
.ThousandsSeparator = "'" 
.UseSystemSeparators = False  
End With

需要检查十进制分隔符是否不等于“ . ”那么我们需要强制使用“ . ” aS小数分隔符 .

请使用VBA或使用Datavalidation来帮助您实现此目的
非常感谢你提前!

1 回答

  • 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
    

相关问题