Getdata procedure returns extra data specially when copying empty cells

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
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top