簡體   English   中英

VBA:CDbl 忽略逗號小數點分隔符

[英]VBA: CDbl ignores comma decimal separator

我正在運行一個宏,將特定的單元格值從電子表格復制到另一個。 這些文件是 csv,以分號分隔,包含通用格式的單元格,包括文字項和數字項。 數字項以逗號作為小數點分隔符。 該腳本正確導入所有內容,但是,它在導入數值時忽略逗號。

這是代碼:

Public Function importCsv2(strPath As String)
Dim filePath As String
filePath = strPath
Dim fileArray(5, 5) As String
Dim startDate As String
Dim Product As String
Application.DecimalSeparator = ","
If Range("B14").Value <> "" Then
    Range("B13").End(xlDown).offset(1, 0).Select
Else:
    Range("B13:B" & Range("B" & Rows.Count).End(xlUp).Row).offset(1, 0).Select
End If
currentRow = 0
rowNumber = 0

Open filePath For Input As #1

Do Until EOF(1)
    Line Input #1, lineFromFile
    fileStr = Split(lineFromFile, vbLf)
    Dim item As Variant
    For Each item In fileStr
    'For item = LBound(fileStr) To UBound(fileStr)
        lineitems = Split(item, ";")
        'Debug.Print (item)
        If rowNumber = 1 Then
            startDate = lineitems(6)
            Product = lineitems(9)
        End If
        If rowNumber > 3 And item <> "" Then

            If Not doesOfferExist(CStr(lineitems(2))) And CInt(lineitems(0)) <> 0 Then
                ActiveCell.offset(currentRow, 0) = startDate
                ActiveCell.offset(currentRow, 1) = lineitems(4)
                ActiveCell.offset(currentRow, 2) = lineitems(3)
                ActiveCell.offset(currentRow, 3) = CDbl(lineitems(6))
                ActiveCell.offset(currentRow, 4) = CDbl(lineitems(7))
                ActiveCell.offset(currentRow, 5) = lineitems(8)
                ActiveCell.offset(currentRow, 6) = lineitems(1)
                ActiveCell.offset(currentRow, 7) = lineitems(2)
                ActiveCell.offset(currentRow, 8) = "New"
                ActiveCell.offset(currentRow, 9) = Product
                currentRow = currentRow + 1
            End If
        End If


    Next item
    rowNumber = rowNumber + 1
Loop
Close #1

Call setImportLastUpdate
End Function

關鍵部分是:

ActiveCell.offset(currentRow, 3) = CDbl(lineitems(6))
ActiveCell.offset(currentRow, 4) = CDbl(lineitems(7))

excel 選項將逗號設置為小數點分隔符。

例如,當導入 84,55 時,我得到 8455 結果。 我添加了Application.DecimalSeparator = ","但它沒有解決問題。

有什么幫助嗎? 先感謝您

有點晚,但供將來參考。

像這樣設置千位分隔符小數分隔符

Application.ThousandsSeparator = "."
Application.DecimalSeparator = ","

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM