2013-08-31 87 views
0

如何將存儲爲文本的數字轉換爲數字?將存儲爲文本的數字轉換爲數字?

我已經嘗試設置:

ActiveSheet.Range("H154").NumberFormat = "General" 

但它不工作!

我唯一發現的工作是使用「文本到列」或單擊單元格對其進行編輯,然後單擊「輸入」。

但我真的很想找到一種方法,使用VBA將存儲爲文本的工作表中的數字單元格變爲數字。

+1

* *正好也細胞包含什麼? (**正好**,不是「這樣的事情」)。沒有任何信息,真的很難給你一個具體的答案。如果您的數據或格式可以阻止它,則應用「常規」格式將不起作用,但我們無法判斷您是否不向我們顯示數據。 –

回答

3

的一般方法是複製PasteSpecial的,乘以1

在代碼中,這樣的事情:

Sub ConvertToNumber() 
    Dim rng As Range 
    Dim cl As Range 
    Dim rConst As Range 

    ' pick an unused cell 
    Set rConst = Cells(1, 4) 
    rConst = 1 

    Set rng = Cells.SpecialCells(xlCellTypeConstants) 
    rng.NumberFormat = "General" 
    rConst.Copy 
    rng.PasteSpecial xlPasteValues, xlPasteSpecialOperationMultiply 

    rConst.Clear 
End Sub 
0

只需使用CDbl()

ActiveSheet.Range("H154") = CDbl(ActiveSheet.Range("H154")) 
+1

@ sjas:真的看不到你的輸入帶給線程。 – ProtoVB

2

我不是編碼專家和「存儲爲文本的數字」錯誤困擾了我很長一段時間。

我終於發現了這一點: Delimited Text-to-Columns in a Macro

這讓我這個:

Sub ConvertTextToNumber() 
     Sheets("Worksheet_Name").Select 
     Range("A1").Select 
     Selection.TextToColumns _ 
      Destination:=Range("A:A"), _ 
      DataType:=xlDelimited 
    End Sub 

我用這個在宏在一個新的工作表複製&重新排序列:

Sub ColumnReorder() 
    '********************************************************** 
    'Paste this macro into the Workbook of each new "Employee_List_Weekly_Update" 
    'Functionality: 
    '1. Column order in the "Employee_List_Weekly_Update" worksheet changes fairly often. 
    ' The macro will find each column by header name, 
    ' select that column and copy it to the new sheet. 
    '2. The macro also converts "Employee ID#" to a number, 
    ' removing the "Number saved as Text" error. 
    '********************************************************** 
    'Create new sheet 
     Sheets.Add.Name = "Roster_Columns_Reordered" 

    'Repeat for each column or range 
    'Find Column in "Employee_List_Weekly_Update" - Copy it - Paste it in "Roster_Columns_Reordered" - Employee ID# 
     Dim a As Integer 
     Sheets("Employee_List_Weekly_Update").Select 
     Set rngData = Range("A1").CurrentRegion 
     a = Application.WorksheetFunction.Match("Employee ID#", Range("A1:BB1"), 0) 
     Columns(a).Select 
     Selection.Copy 

     Sheets("Roster_Columns_Reordered").Select 
     Range("A1").Select 
     ActiveSheet.Paste 
    'Use TextToColumns to convert "Number Stored as Text " 
     Selection.TextToColumns _ 
      Destination:=Range("A:A"), _ 
      DataType:=xlDelimited 

    'Find Column in "Employee_List_Weekly_Update" - Copy it - Paste it in "Roster_Columns_Reordered" - Name 
     Dim b As Integer 
     Sheets("Employee_List_Weekly_Update").Select 
     Set rngData = Range("A1").CurrentRegion 
     b = Application.WorksheetFunction.Match("Name", Range("A1:BB1"), 0) 
     Columns(b).Select 
     Selection.Copy 

     Sheets("Roster_Columns_Reordered").Select 
     Range("B1").Select 
     ActiveSheet.Paste 

    'Go to "Roster_Columns_Reordered" - Add AutoFilter - Freeze Top Row 
     Rows("1:1").Select 
     Selection.AutoFilter 
     With ActiveWindow 
      .SplitColumn = 2 
      .SplitRow = 1 
     End With 
     Rows("2:2").Select 
     ActiveWindow.FreezePanes = True 
     Range("A1").Select 

    End Sub