2016-01-20 57 views
2

我有多種格式的數據,並且我努力改變它們以嘗試找到它們。所討論的數字目前以dataT.Range("F2:F" & lRow).NumberFormat = "###.00"的格式存儲爲數字。但是,它們存儲在另一個電子表格的其他地方,其格式爲####,不包括小數。這樣的例子將是原始格式:「30.00」新格式「3000」或原始格式:「10.50」新格式「1050」。我試圖通過Replace()函數刪除小數,但我相當肯定它會失敗,它確實如此。任何想法或建議將不勝感激。這個功能是一個更大程序中非常小的一部分。發現不同格式的長文字

問題簡單地說:我需要改變以這種格式存儲「30.00」目標格式「3000」與開始行權是一個數字,我試圖解決這個問題

Function AnalyzeOiData(lRow As Integer, oiDataSheet As Worksheet, productSheet As Worksheet, rownum As Integer) 

Dim counter As Integer 
Dim monthExp As String 
Dim oiLocationFinder As Range 
Dim strikeLoc As Range 
Dim optStrike As Long 

For counter = rownum To lRow 

    'Returns formatted monthcode for finding the different months of expiry embedded in the OI data 
    monthExp = GetMonthCode(productSheet.Cells(counter, 1), productSheet.Cells(counter, 2)) 
    optStrike = Replace(productSheet.Cells(counter, 3), ".", "") *** 
    oiLocationFinder = oiDataSheet.Columns(1).Find(monthExp) 

    oiLocationFinder.Select 
    strikeLoc = Range(Selection, Selection.End(xlDown)).Find(optStrike).Address 

    productSheet.Cells(counter, 11) = strikeLoc.Offset(0, 9) 
    productSheet.Cells(counter, 12) = strikeLoc.Offset(0, 10) 

Next 

End Function 
+5

'10.50 * 100 = 1050'? –

+0

請修復您的文章中的代碼的格式,謝謝:) – Archimaredes

+0

剛剛去做,斯科特做到了。當我複製它時,它會被拋棄。 – StormsEdge

回答

2

輸入您要加工成以下子每個單元:

Sub Convert(cell As Range) 
    If cell.NumberFormat = "0.00" Then 
     cell.Value = cell.Value * 100 
     cell.NumberFormat = "0" 
    End If 
End Sub 

如果你給這些細胞中......

Original data

...結果是:

Processed data

0

爲拓寬答案「乘以100」到更多的東西一般情況下,下面的代碼會看看,看看有多少位小數有,然後乘以10的正確因子以除去小數。

Dim iDec as Integer 
If Instr(1,productSheet.Cells(counter,3),".") > 0 Then 
    iDec = Len(productSheet.Cells(counter,3))-Application.WorksheetFunction.Find(".",productSheet.Cells(counter,3)) 
    productSheet.Cells(counter,3) = productSheet.Cells(counter,3) * (10^iDec) 
End If 
-3

我不是100%肯定,我不任何地方,我現在就可以考,但會

範圍( 「F2」)。值2 * 100

工作這裏?它應該給沒有格式的基礎價值,不是?

+1

對於「30.00」的輸入,這將按照要求給出「30」,而不是「3000」。 – nwhaught

1

您可以運行與Find高效的搜索,而不是看每個單元:

Dim rng1 As Range 
Application.FindFormat.NumberFormat = "0.00" 

Set rng1 = Range("A1:A10").Find("*", , , , , , , , True) 
Do While Not rng1 Is Nothing 
    rng1.Value2 = rng1.Value2 * 100 
    rng1.NumberFormat = "0" 
    Set rng1 = Range("A1:A10").Find("*", , , , , , , , True) 
Loop