【发布时间】:2015-11-30 00:37:59
【问题描述】:
我正在寻找一种将文本转换为数字的方法,然后可以在其他公式中进一步使用。我的专栏看起来很简单:
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
但是,除了看到正确的类型之外,Excel 底部只显示了计数。
任何建议如何正确地将 Excel 列转换为数字,然后其他公式可以进一步使用?
我的错误是什么?
更新在这里您可以找到完整的 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
【问题讨论】:
-
VALUE() 函数可以在公式中提取相邻列中的文本 - 如果您有数字和非数字数据的混合,它是有弹性的。 .如果做不到这一点,则抓取 VBA 数组中的列,循环遍历数组,将 Clng() 或 Cdb() 应用于每个成员,然后将数组写回范围。
-
是的,我有点困惑。你说“除了显示正确的类型......”所以单元格的内容
IsNumeric?如果是这样,那么您的单元格是一个数字,而问题是“在 Excel 底部显示计数”的公式。但是@Nile 是正确的。.Value应将任何数字文本视为数字。 -
@pnuts 你的方法效果很好。你能解释一下为什么吗?我将不胜感激!
标签: vba excel formatting excel-2010