2017-10-10 446 views
3

我有1列,它應該包含一個數量,但它有不正確的格式,它被視爲文本。VBA - 替換字符串中的字符

enter image description here

因此,有必要更換白白點,然後用逗號的點。我有代碼:

Private Sub Correction_of_dot() 
    Dim i As String 
    Dim k As String 
    i = "." 
    k = "" 
    Columns("P:P").Replace what:=i, replacement:=k, lookat:=xlPart, MatchCase:=False 
End Sub 

Private Sub Correction_of_comma() 
    Dim i As String 
    Dim k As String 
    i = "," 
    k = "." 
    Columns("P:P").Replace what:=i, replacement:=k, lookat:=xlPart, MatchCase:=False 
End Sub 

但它什麼也沒做...沒有錯誤,只是加載,然後什麼也沒有發生。你能告訴我,我做錯了什麼,或者我能做得更好嗎?

非常感謝!

+0

何時以及如何調用這些子?只是將列數據類型更改爲所需的格式會不會更容易? – Quirinux

+0

該代碼顯示正確,但可能值得明確聲明工作表,例如Worksheets(「Sheet1」)。Columns(「P」)... – Leroy

回答

0

按我的區域設置你的數據,點作爲小數點分隔符和逗號用作千位分隔符。話雖如此,下面的代碼爲我工作,併產生預期的輸出。

爲了測試代碼,我複製了網頁上的數字並粘貼在工作表上,它們看起來像下面那樣...

enter image description here

代碼:

Sub Test() 
With Range("P:P") 
    .Replace ".", "" 
    .Replace ",", "." 
    .NumberFormat = "0.00" 
End With 
End Sub 

輸出:

enter image description here

0

這是一個辦法做到這一點:

Public Sub TestMe() 

    Debug.Print generateNumber("1.525,32") 
    'works with mixed formats: 
    Debug.Print generateNumber("12,525.33") 
    Debug.Print generateNumber("1,112.525,35") 

End Sub 

Public Function generateNumber(strInput As String) As Double 

    strInput = Replace(Replace(strInput, ".", ""), ",", "") 

    generateNumber = CDbl(Left(strInput, Len(strInput) - 2)) 

    'getting the first digit after the comma 
    generateNumber = generateNumber + 0.1 * CLng(Mid(strInput, Len(strInput) - 1, 1)) 

    'getting the second digit after the comma 
    generateNumber = generateNumber + 0.01 * CLng(Mid(strInput, Len(strInput), 1)) 

End Function 

如果輸入的是標準的,與整個號碼後2位,它會返回一個標準輸出。

它有一個小的加格式,因爲在不同的國家,分隔符是不同的。

0

你的代碼適合我,如果它在你的情況下不起作用,我認爲該列被格式化爲文本;只需將格式更改爲數字或標準。

試試這個:

Private Sub Correction_of_dot() 
    Columns("P:P").Select 
    Selection.NumberFormat = "General" 

    Dim i As String 
    Dim k As String 
    i = "." 
    k = "" 
    Columns("P:P").Replace what:=i, replacement:=k, lookat:=xlPart, MatchCase:=False 

    i = "," 
    k = "." 
    Columns("P:P").Replace what:=i, replacement:=k, lookat:=xlPart, MatchCase:=False 
End Sub 
0

,不需要宏觀干預,簡單的辦法是選擇具有文本值,轉到Data標籤欄==>Text to Columns ==>下一步==>下一步==>選擇General列數據格式。

enter image description here

enter image description here

enter image description here

你也可以告訴Excel中如果逗號是小數點或千個分隔符(在高級選項)...

enter image description here

+1

由於混合使用小數點分隔符 – Tom

+0

,作品,如果它不能自動識別,你可以隨時使用高級文本設置... –

+1

沒有在我的測試中工作,但沒有嘗試使用我看到你現在的'高級文本導入設置「添加 – Tom

1

嘗試使用這個。代碼首先更改語言以匹配列中的小數點分隔符。這使得Excel將它識別爲存儲爲文本的數字。然後,它使用text to columns重置小數(和千)分隔符迴應該是什麼之前至數字轉換 - 離開現在存儲爲數字

Sub CorrectIncorrectNumberFormat() 
    With Application 
     .UseSystemSeparators = False 
     .DecimalSeparator = "," 
     .ThousandsSeparator = "." 
    End With 

    'Update to your applicable range I've set it to sheet1 Column A 
    With Sheet1 
     With Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1)) 
      .TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _ 
       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ 
       Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _ 
       :=Array(1, 1), TrailingMinusNumbers:=True 
     End With 
    End With 

    With Application 
     .DecimalSeparator = "." 
     .ThousandsSeparator = "," 
     .UseSystemSeparators = True 
    End With 
End Sub 
0

你可以遍歷列,並使用替換功能。

Dim i As Long 
Dim finalRow As Long 

finalRow = SheetX.Cells(Rows.Count, 1).End(xlUp).Row 

For i = 1 to finalRow 

    SheetX.Cells(i, 1).Value = Replace(SheetX.Cells(i, 1).Value, ".", "") 
    SheetX.Cells(i, 1).Value = Replace(SheetX.Cells(i, 1).Value, ",", ".") 

Next i 

注:我沒有測試 - 但它應該工作。 另請參見:將SheetX更改爲適當的CodeName,並根據需要更改列參考(本例中的第1列)