将导入的列从文本字段转换为数字

我正在寻找一种方法来将文本转换为数字,然后可以在其他公式中进一步使用。 我的列看起来很简单:

Data1 Data2 1 1 123 1324 1000 1000 ... ... 

我尝试了几种方法:

 Range("S:S").NumberFormat = "0" Range("T:T").NumberFormat = "0" 

或复制一个空单元格,并将其添加到范围:

 Sheets("Sold_Portfolio").Range("ZZ100000").Copy Sheets("Sold_Portfolio").Range("T3:T100000").PasteSpecial , xlPasteSpecialOperationAd 

但是,除了看到正确的types,只显示在Excel底部的计数。

任何build议如何正确地将Excel列转换为数字,然后可以由其他公式进一步使用?

我的错误是什么?

更新在这里你可以find完整的VBA代码:

 Sub selectFile() Dim wbI As Workbook, wbO As Workbook Dim wsI As Worksheet Set wbI = ThisWorkbook 'sheet where you want to import the sheet in Set wsI = wbI.Sheets("Sheet_Items") ChDir (Environ("USERPROFILE") & "\Desktop") 'Select the file Fname = Application.GetOpenFilename(filefilter:="Text Files (*.txt),*.txt", MultiSelect:=False) ' check if file is selected If Fname = False Then ' MsgBox "No File Was Selected" Exit Sub End If ' delete the current content there Sheets("Sheet_Items").Range("G3:AB50000").ClearContents 'Set wbO = Workbooks.Open(Fname) ' 'wbO.Sheets(1).Cells.Copy wsI.Cells ' 'wbO.Close SaveChanges:=False Dim var As String var = "TEXT;" & Fname With ActiveSheet.QueryTables.Add(Connection:= _ var, Destination:=Range("$G$3") _ ) .Name = "Sample" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = True .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With 'You need to properly qualify the range for the Replace() method Columns("S").Replace What:=".", _ Replacement:=",", _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ MatchCase:=False, _ SearchFormat:=False, _ ReplaceFormat:=False Columns("T").Replace What:=".", _ Replacement:=",", _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ MatchCase:=False, _ SearchFormat:=False, _ ReplaceFormat:=False 'Range("S3:S100000") = Range("S3:S100000").Value 'Range("T3:T100000") = Range("T3:T100000").Value ' 'convert to number 'for further details on other available formats have a look at: http://stackoverflow.com/questions/20648149/what-are-numberformat-options-in-excel-vba 'Range("S:S").NumberFormat = "0" 'Range("T:T").NumberFormat = "0" ''get an emtpy cell 'Sheets("Sheet_Items").Range("ZZ100000").Copy 'Sheets("Sheet_Items").Range("S3:S100000").PasteSpecial , xlPasteSpecialOperationAdd ' 'Sheets("Sheet_Items").Range("ZZ100000").Copy 'Sheets("Sheet_Items").Range("T3:T100000").PasteSpecial , xlPasteSpecialOperationAdd 'select the cell B1 Sheets("Sheet_Items").Range("B1").Select End Sub 

你可能运行如下的东西:

 Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, Tab:=True, FieldInfo:=Array(1, 1) 

在每列(这里假设为A)。

文本到列是必须由专有代码支持的向导。 其中一项function是允许select“导入”数据的格式,然后将其写入现有列并parsing到其他列(其中(a)是空的(除非该要求被覆盖)和(b )在选定的分隔符的右边或者给定的固定宽度之外有数据)。

“导入的”数据实际上已经在选定的列中。 由于在这种情况下,不包含任何选项卡,因此selectTab作为分隔符意味着不涉及任何分析 – 数据将从列中读取并重新input到其中。 然而,作为重新input过程的一部分,格式可能会改变。 在这种情况下, array的第二1参数定义数字。

专有代码通常比VBA快得多。

考虑:

 Sub qwerty() Dim r As Range, rng As Range Set rng = Intersect(Range("S:T"), ActiveSheet.UsedRange) rng.ClearFormats For Each r In rng r.Value = r.Value Next r End Sub