简体   繁体   中英

How do I get the data type for a number stored as text, exported from Access?

I have a file that was exported from an Access database, and in this file there is a column for the software revision. When it gets exported to Excel, the software revision turns into a "number stored as text". In this case it is the text "11.3."

I would like to check that these values are all the same, but I cannot get my code to recognize the text value of 11.3. I tried changing the values to a number type, and comparing it to a number value, but that is not working. It always goes to my Else statement to close the macro.

Here is my code (go to 'Check for SW Rev; this is where the error occurs):

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

I don't have time right now to figure out where you are making an error. My best guess is that your range returned by Selection is not what you think it should be.

But the following code converts all of the text values on your downloaded worksheet to numeric values:

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

This is a dumb one, but it was a row/column mixup. In the following line, under the 'Check for Correct SW Rev, I had iCounter and iSoft mixed up.

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

I also changed it back to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM