Something's happening to the strings when I pick them up via ADODB connection. This really doesn't make any sense.
Ok, I collect the values in the function and pass them back. Then, I search for the first string in the array and I get no matches. But, If I manually assign the Var(0) to be the exact same as it was before, everything works just fine!
Here's a screenshot: http://img641.imageshack.us/img641/4043/unledozi.png
So, something is happening here. Here's my full code that's a bit altered from your last one. Notice how I specifically collect only the three cells I want the value from in that function you created. Then I merge these to an array, which is passed back to the initial sub.
Public Sub collectData()
Dim ws As Worksheet
Dim strDir As String, strFileName As String
Dim rngSearchRange As Range, rngFindRange As Range
Dim wbReadBook As Workbook
Dim CalcState
Dim Var As Variant
Set ws = ThisWorkbook.Sheets(1)
Application.ScreenUpdating = False
CalcState = Application.Calculation
Application.Calculation = xlCalculationManual
'On Error GoTo ErrorHandler
strDir = "C:\Users\vsando\Documents\Mine Prosjekter\Arbeidsklær\"
strFileName = Dir(strDir & "*.xls")
' Specifies the sheet name and area within the summary workbook
' to look for string in B6 within each workbook, column A in this example
Set rngSearchRange = ws.Range(ws.[a2], ws.Cells(Rows.Count, "A").End(xlUp))
' Loop through workbooks
Do While strFileName <> ""
Var = returnResults(strDir & strFileName)
If VarType(Var) = vbBoolean Then Err.Raise 75
' Find string in B6 within summary sheet
Set rngFindRange = rngSearchRange.Find(Var(0), LookIn:=xlValues, lookat:=xlWhole)
' No results are found, even though the value of Var(0) is "apples bananas"
Var(0) = "apples bananas"
' Now it can find it just fine!
Set rngFindRange = rngSearchRange.Find(Var(0), LookIn:=xlValues, lookat:=xlWhole)
If Not rngFindRange Is Nothing Then
MsgBox Var(0) & " found in" & rngFindRange.Address(0, 0)
Else
MsgBox Var(0) & " not found in:"
End If
'Set rngFindRange = rngSearchRange.Find(Range("B6"), LookIn:=xlValues, lookat:=xlWhole)
' If found populate adjacent cells or issue warning
If Not rngFindRange Is Nothing Then
rngFindRange.Offset(0, 1) = Val(Var(0, 0))
rngFindRange.Offset(0, 2) = Val(Var(0, 6))
Else
'AddRows (Val(var(0, 0)))
'AddRows (Val(var(0, 6)))
MsgBox "No match found for " & strFileName, vbExclamation
End If
strFileName = Dir
Loop
CleanExit:
Application.ScreenUpdating = True
Application.Calculation = CalcState
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Name, vbCritical, "Error"
GoTo CleanExit
End Sub
Function returnResults(FilePath As String) As Variant
Dim mergedArray(2) As Variant
Dim strNavn As String
Dim intSommer As Integer
Dim intVinter As Integer
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Const strQueryName = "SELECT * FROM [B5:B6];"
Const strQuerySommer = "SELECT * FROM [B48:B49];"
Const strQueryVinter = "SELECT * FROM [B54:B55];"
Const strQuery As String = "SELECT * FROM [B5:B54];"
'On Error GoTo Handler
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & FilePath & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
Set rs = New ADODB.Recordset
rs.Open strQueryName, cn, adOpenStatic, adLockReadOnly
strNavn = rs.GetString
rs.Close
rs.Open strQuerySommer, cn, adOpenStatic, adLockReadOnly
intSommer = rs.GetString
rs.Close
rs.Open strQueryVinter, cn, adOpenStatic, adLockReadOnly
intVinter = rs.GetString
mergedArray(0) = strNavn
mergedArray(1) = intSommer
mergedArray(2) = intVinter
returnResults = mergedArray
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
Exit Function
Handler:
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
returnResults = False
End Function
Edit: What's really strange here is that if I take the value from Var(0) and assign that value to one of the cells in the Excel sheet and then re-run the code, it can find it!
Even though it looks exactly the same. I just use "ws.Cells(5,1).Value = Var(0)" and see that the string is the exact same. But somehow Var(0) differs. I've also checked if there are any white spaces and there are none. Also, both cells are formatted exactly the same.
Bookmarks