Here is what I did. If a column is flagged for checking then if it is entirely numeric the only nonnumeric row should be row1 which is the column header. I used specialcells and counted the text areas and then used it again to count the numeric rows.
I used the areas count for the strings because rows only returns the rows for the first area (if more than 1) which could be misleading. It seems to be working just fine.
Then after the import I checked row 1 for the specified fields by spinning through the ColumnsAffected array and stripped off the string I had prepended to force it to text. Merely prepending an Excel string indicator did not seem to work.
Sorry for the ugly formatting but I left it alone so it would hopefully paste properly into a module if someone wanted to use it. If it gives you trouble, post an email address and I'll try to send it to you.
Thanks for the help and suggestions. Hopefully this will help save someone the grief I encountered.
************* Begin Code *******************8
Public Sub Sub_ValidateImportColumnTypes(pxlWorkSheet As Worksheet, _
ByRef ravarColumnsAffected As Variant)
Dim xlWorkSheet As Worksheet
Dim rng As Range
Dim lngCol As Long
Dim lngRow As Long
Dim lngText As Long
Dim lngSectionCount As Long
Dim lngNumeric As Long
Dim lngBlanks As Long
Dim blnNonNumeric As Boolean
Dim strText As String
On Error Resume Next 'No cells were found. - Raises 1004 error
strText = "ForceToString"
'Activate requested Worksheet
pxlWorkSheet.Activate
'Just returns specialcells(xlLast)
lngRow = Func_GetLastRow(pxlWorkSheet)
'Spin through all of the columns in the worksheet
For lngCol = 1 To pxlWorkSheet.Columns.Count
'Does the current column have anything in row 1
If Len(pxlWorkSheet.Cells(1, lngCol)) > 0 Then
'debug MsgBox lngCol & " ~" & pxlWorkSheet.Cells(1, lngCol) & "~" 'debug
'Ensure no leading spaces in column names
pxlWorkSheet.Cells(1, lngCol) = Trim$(pxlWorkSheet.Cells(1, lngCol))
pxlWorkSheet.Range(Cells(1, lngCol), Cells(lngRow, lngCol)).Activate
With pxlWorkSheet.Range(Cells(1, lngCol), Cells(lngRow, lngCol))
'NOTE: Remember that if multiple sections are returned by the range
' assignment below then the row counts only refer to secion 1
'Count the Text cells
Set rng = .SpecialCells(xlCellTypeConstants, xlTextValues)
If Err.Number = 0 Then
lngText = rng.Rows.Count
lngSectionCount = rng.Areas.Count
'If more than 1 text section or more than 1 text row then text
If lngText > 1 Or lngSectionCount > 1 Then
blnNonNumeric = True
Else
blnNonNumeric = False
End If
'debug MsgBox intX & " " & rng.Address & " " & rng.Areas.Count 'debug
Else
Err.Clear
lngText = 0
End If
'Count the Numeric cells
Set rng = .SpecialCells(xlCellTypeConstants, xlNumbers)
If Err.Number = 0 Then
lngNumeric = rng.Rows.Count
'debug MsgBox intX & " " & rng.Address & " " & rng.Areas.Count 'debug
Else
Err.Clear
lngNumeric = 0
End If
'Count the Blank cells
' Set rng = .SpecialCells(xlCellTypeBlanks)
' If Err.Number = 0 Then
' lngBlanks = rng.Rows.Count
'debug MsgBox intX & " " & rng.Address & " " & rng.Areas.Count 'debug
' Else
' Err.Clear
' lngBlanks = 0
' End If
'if mixed data found in the column then ensure row 2 is text to force text import
If blnNonNumeric And lngNumeric > 0 Then
If IsNumeric(.Cells(2, lngCol)) Then
'Force column to be treated as text by prepending a text string to it
pxlWorkSheet.Cells(2, lngCol).Activate
pxlWorkSheet.Cells(2, lngCol) = strText & pxlWorkSheet.Cells(2, lngCol)
'Save this column header in the ColumnsAffected array
If IsEmpty(ravarColumnsAffected) Then
ReDim ravarColumnsAffected(1, 0)
Else
ReDim Preserve ravarColumnsAffected(1, UBound(ravarColumnsAffected, 2) + 1)
End If
'NOTE: Do not change the following string as it is used elsewhere in the code
ravarColumnsAffected(0, UBound(ravarColumnsAffected, 2)) = "Affected Column"
ravarColumnsAffected(1, UBound(ravarColumnsAffected, 2)) = _
pxlWorkSheet.Cells(1, lngCol).Value
End If
End If
End With
End If
Next lngCol
End Sub 'Sub_ValidateImportColumnTypes
***************** End Code *************************