如何获取以文本forms存储的数字的数据types,从Access中导出?

我有一个从Access数据库导出的文件,在这个文件中有一个软件版本的列。 当它被导出到Excel时,软件版本变成“存储为文本的数字”。 在这种情况下,文本是“11.3”。

我想检查这些值是否都是一样的,但我不能让我的代码识别11.3的文本值。 我试着将值更改为数字types,并将其与数字值进行比较,但这不起作用。 它总是去我的Else声明closuresmacros观。

这里是我的代码(转到“检查软件版本,这是发生错误的地方):

Private Sub Main() 'Program comments 'Variable definition Dim wBook As Workbook Dim sBook As String Dim rRange As Range Dim iSoft As Long Dim iCounter As Long Dim iLastRow As Long Dim sTemp As String Dim dTemp As Double 'Open the Excel file sBook = Application.GetOpenFilename() If sBook = "False" Then End End If Set wBook = Workbooks.Open(sBook) 'Check if PartData sheet exists If CheckSheet("PartData") = False Then MsgBox "Your data does not include the adjuster 'PartData' sheet." MsgBox "The macro is aborting now." wBook.Close End End If 'Check for correct number of samples With wBook.Worksheets("PartData") Set rRange = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious) If Not rRange Is Nothing Then iLastRow = rRange.Column Else MsgBox "No data. Aborting." End End If If Not iLastRow = 91 Then MsgBox "Incorrect number of samples. Aborting." End If End With 'Find Version Column With wBook.Worksheets("PartData").Range("A1:DD1") Set rRange = .Find("SW Rev", LookIn:=xlValues) If Not rRange Is Nothing Then iSoft = rRange.Column Else MsgBox "Software Revision column 'SW Rev' Not found. Aborting." End End If End With 'Convert SW Rev to numbers sTemp = ConvertToLetter(iSoft) wBook.Worksheets("PartData").Range(sTemp & ":" & sTemp).Select With Selection Selection.NumberFormat = "0.0" .Value = .Value End With 'Check for Correct SW Rev dTemp = 11.3 For iCounter = 2 To iLastRow If wBook.Worksheets("PartData").Cells(iSoft, iCounter).Text = dTemp Then 'Do Nothing Else 'Incorrect SW Rev MsgBox "Incorrect SW Rev. Aborting." wBook.Close End End If Next iCounter wBook.Close End Sub Function CheckSheet(ByVal sSheetName As String) As Boolean Dim oSheet As Excel.Worksheet Dim bReturn As Boolean For Each oSheet In ActiveWorkbook.Sheets If oSheet.Name = sSheetName Then bReturn = True Exit For End If Next oSheet CheckSheet = bReturn End Function Function ConvertToLetter(iCol As Long) As String Dim iAlpha As Integer Dim iRemainder As Integer iAlpha = Int(iCol / 27) iRemainder = iCol - (iAlpha * 26) If iAlpha > 0 Then ConvertToLetter = Chr(iAlpha + 64) End If If iRemainder > 0 Then ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64) End If End Function 

我现在没有时间弄清楚你在哪里犯错误。 我最好的猜测是你Selection的范围不是你想象的那样。

但是,下面的代码将您下载的工作表上的所有文本值转换为数字值:

 Option Explicit Sub ConvertNumbers() Dim R As Range Set R = Cells.Find(what:="SW Rev", after:=Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True) Set R = Range(R.Offset(1, 0), Cells(Rows.Count, R.Column).End(xlUp)) R = R.Value End Sub 

这是一个愚蠢的,但它是一个行/列混合。 在下面的行中,在“检查正确的软件修订版”下,我将iCounter和iSoft混淆了。

 If wBook.Worksheets("PartData").Cells(iSoft, iCounter).Text = dTemp Then 

我也把它改回

 If wBook.Worksheets("PartData").Cells(iSoft, iCounter).Text = "11.3" Then