[英]How do I get the data type for a number stored as text, exported from Access?
我有一個從Access數據庫導出的文件,並且在此文件中有一列用於軟件修訂。 導出到Excel后,軟件修訂版將變成“數字存儲為文本”。 在這種情況下,它是文本“ 11.3”。
我想檢查一下這些值是否都相同,但是我無法讓我的代碼識別11.3的文本值。 我嘗試將值更改為數字類型,然后將其與數字值進行比較,但這不起作用。 它總是轉到我的Else語句來關閉宏。
這是我的代碼(轉到“檢查軟件修訂版;這是發生錯誤的地方):
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.