tinasomot
New Member
- Joined
- Jan 20, 2008
- Messages
- 2
Code Example:
Sub getLookup()
GetData "\\myserver\SourceFile.xls", _
"2015", "A3:AW4", Worksheets("Lookup").Cells(3, 1), True
end Sub
I use the getdata several times to copy a range of data from a closed workbook to my current workbook and it seems to work fine for me in Excel 2010 except for this particular range on my source spreadsheet "2015", the getdata returns the actual values from the source file and for cells that are empty (B3, D3, F3) , it returns values like F2, F4, F6 .... The digits appear to be corresponding to the column numbers. I check the source file (in sheet "2015", row 3) cell B3, D3, F3 appear to be empty.
It even shows F1 on cell (4,A) where the source file has an actual value in that cell. What are those F1, F2, F4, F6 ? Where do they come from?
Randomly, it appends a digit like 1, 2, 3 to a couple values on the destination sheet too. Any idea how I can fix this issue?
Thank you,
Attached is the code for the getData procedure.
Sub GetData(SrcFile$, SrcSheet$, SrcRange$, rTgt As Range, fHdr As Boolean)
'Called by Get_Lookup()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim a&
Dim cnct$
'Initialize a variable for the connection string
cnct$ = "DRIVER={Microsoft Excel Driver (*.xls)}; DBQ=" & SrcFile$
'Initialize connection object
Set cn = New ADODB.Connection
With cn
'Open the database connection
.Open cnct$
'Execute the query
Set rs = .Execute("[" & SrcSheet$ & "$" & SrcRange$ & "]")
End With
'Initialize a variable for the upper left cell of the target range
Set rTgt = rTgt.Cells(1)
With rs
'Determine whether to get the field header
If fHdr Then
'Loop across the fields
For a& = 0 To .Fields.Count - 1
'Get the field names
rTgt.Offset(0, a&).Value = .Fields(a&).name
Next a&
'Advance the target pointer to the next available row in the destination worksheet
Set rTgt = rTgt.Offset(1, 0)
End If
'Apply the CopyFromRecordset method
rTgt.CopyFromRecordset rs
'Close the RecordSet
.Close
End With
'Close the database connection
cn.Close
'Recover memory from object variables
Set cn = Nothing
Set rs = Nothing
End Sub
Thank you
Sub getLookup()
GetData "\\myserver\SourceFile.xls", _
"2015", "A3:AW4", Worksheets("Lookup").Cells(3, 1), True
end Sub
I use the getdata several times to copy a range of data from a closed workbook to my current workbook and it seems to work fine for me in Excel 2010 except for this particular range on my source spreadsheet "2015", the getdata returns the actual values from the source file and for cells that are empty (B3, D3, F3) , it returns values like F2, F4, F6 .... The digits appear to be corresponding to the column numbers. I check the source file (in sheet "2015", row 3) cell B3, D3, F3 appear to be empty.
It even shows F1 on cell (4,A) where the source file has an actual value in that cell. What are those F1, F2, F4, F6 ? Where do they come from?
Randomly, it appends a digit like 1, 2, 3 to a couple values on the destination sheet too. Any idea how I can fix this issue?
Thank you,
Attached is the code for the getData procedure.
Sub GetData(SrcFile$, SrcSheet$, SrcRange$, rTgt As Range, fHdr As Boolean)
'Called by Get_Lookup()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim a&
Dim cnct$
'Initialize a variable for the connection string
cnct$ = "DRIVER={Microsoft Excel Driver (*.xls)}; DBQ=" & SrcFile$
'Initialize connection object
Set cn = New ADODB.Connection
With cn
'Open the database connection
.Open cnct$
'Execute the query
Set rs = .Execute("[" & SrcSheet$ & "$" & SrcRange$ & "]")
End With
'Initialize a variable for the upper left cell of the target range
Set rTgt = rTgt.Cells(1)
With rs
'Determine whether to get the field header
If fHdr Then
'Loop across the fields
For a& = 0 To .Fields.Count - 1
'Get the field names
rTgt.Offset(0, a&).Value = .Fields(a&).name
Next a&
'Advance the target pointer to the next available row in the destination worksheet
Set rTgt = rTgt.Offset(1, 0)
End If
'Apply the CopyFromRecordset method
rTgt.CopyFromRecordset rs
'Close the RecordSet
.Close
End With
'Close the database connection
cn.Close
'Recover memory from object variables
Set cn = Nothing
Set rs = Nothing
End Sub
Thank you