PC Review


Reply
Thread Tools Rate Thread

Copying the first row of wbooks with ADO

 
 
Geoff
Guest
Posts: n/a
 
      3rd Mar 2008
Hi
I would like to copy the first row of data from a number of wbooks using ADO
but for now the code below is just for 1 source wbook to the Log file.
It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
is installed. The code works fine providing there is data in cell A1. But
if the first cell containing data in the source file (test1) is say C1, then
data is still copied into the Log file starting at A1 whereas it ought to be
C1. Empty columns elsewhere in the source file row1 are copied correctly
How can I rectify this?
Is my approach the best way to go about the process for multiple wbooks?

T.I.A.

Geoff

Option Explicit
Option Private Module

Sub Test2()
GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
End Sub

Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
As _ String)
Dim rs As ADODB.Recordset
Dim szConnect As String
Dim szSQL As String

szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
szSQL = "SELECT * FROM [Sheet1$];"
Set rs = New ADODB.Recordset
rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
rs.Fields.Count
rs.Close
Set rs = Nothing
End Function


 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      3rd Mar 2008
Try the examples for more workbooks on this page
http://www.rondebruin.nl/ado.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
> Hi
> I would like to copy the first row of data from a number of wbooks using ADO
> but for now the code below is just for 1 source wbook to the Log file.
> It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
> is installed. The code works fine providing there is data in cell A1. But
> if the first cell containing data in the source file (test1) is say C1, then
> data is still copied into the Log file starting at A1 whereas it ought to be
> C1. Empty columns elsewhere in the source file row1 are copied correctly
> How can I rectify this?
> Is my approach the best way to go about the process for multiple wbooks?
>
> T.I.A.
>
> Geoff
>
> Option Explicit
> Option Private Module
>
> Sub Test2()
> GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
> End Sub
>
> Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
> As _ String)
> Dim rs As ADODB.Recordset
> Dim szConnect As String
> Dim szSQL As String
>
> szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & SourceFile & ";" & _
> "Extended Properties=""Excel 8.0;HDR=No"";"
> szSQL = "SELECT * FROM [Sheet1$];"
> Set rs = New ADODB.Recordset
> rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
> Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
> rs.Fields.Count
> rs.Close
> Set rs = Nothing
> End Function
>
>

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      3rd Mar 2008
Thank you for the quick response but that's a lot of code to go through
immediately and establish if it answers my question which is what happens if
the first column of source data is empty? Multiple workbooks comes after
I've settled this matter.

Geoff

"Ron de Bruin" wrote:

> Try the examples for more workbooks on this page
> http://www.rondebruin.nl/ado.htm
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
> > Hi
> > I would like to copy the first row of data from a number of wbooks using ADO
> > but for now the code below is just for 1 source wbook to the Log file.
> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
> > is installed. The code works fine providing there is data in cell A1. But
> > if the first cell containing data in the source file (test1) is say C1, then
> > data is still copied into the Log file starting at A1 whereas it ought to be
> > C1. Empty columns elsewhere in the source file row1 are copied correctly
> > How can I rectify this?
> > Is my approach the best way to go about the process for multiple wbooks?
> >
> > T.I.A.
> >
> > Geoff
> >
> > Option Explicit
> > Option Private Module
> >
> > Sub Test2()
> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
> > End Sub
> >
> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
> > As _ String)
> > Dim rs As ADODB.Recordset
> > Dim szConnect As String
> > Dim szSQL As String
> >
> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> > "Data Source=" & SourceFile & ";" & _
> > "Extended Properties=""Excel 8.0;HDR=No"";"
> > szSQL = "SELECT * FROM [Sheet1$];"
> > Set rs = New ADODB.Recordset
> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
> > rs.Fields.Count
> > rs.Close
> > Set rs = Nothing
> > End Function
> >
> >

>

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      3rd Mar 2008
Use the code in the links on top of the page
You have much more control then and it is always working correct.


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Geoff" <(E-Mail Removed)> wrote in message news:38CB96C5-B07C-4167-B58C-(E-Mail Removed)...
> Thank you for the quick response but that's a lot of code to go through
> immediately and establish if it answers my question which is what happens if
> the first column of source data is empty? Multiple workbooks comes after
> I've settled this matter.
>
> Geoff
>
> "Ron de Bruin" wrote:
>
>> Try the examples for more workbooks on this page
>> http://www.rondebruin.nl/ado.htm
>>
>> --
>>
>> Regards Ron de Bruin
>> http://www.rondebruin.nl/tips.htm
>>
>>
>> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
>> > Hi
>> > I would like to copy the first row of data from a number of wbooks using ADO
>> > but for now the code below is just for 1 source wbook to the Log file.
>> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
>> > is installed. The code works fine providing there is data in cell A1. But
>> > if the first cell containing data in the source file (test1) is say C1, then
>> > data is still copied into the Log file starting at A1 whereas it ought to be
>> > C1. Empty columns elsewhere in the source file row1 are copied correctly
>> > How can I rectify this?
>> > Is my approach the best way to go about the process for multiple wbooks?
>> >
>> > T.I.A.
>> >
>> > Geoff
>> >
>> > Option Explicit
>> > Option Private Module
>> >
>> > Sub Test2()
>> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
>> > End Sub
>> >
>> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
>> > As _ String)
>> > Dim rs As ADODB.Recordset
>> > Dim szConnect As String
>> > Dim szSQL As String
>> >
>> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
>> > "Data Source=" & SourceFile & ";" & _
>> > "Extended Properties=""Excel 8.0;HDR=No"";"
>> > szSQL = "SELECT * FROM [Sheet1$];"
>> > Set rs = New ADODB.Recordset
>> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
>> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
>> > rs.Fields.Count
>> > rs.Close
>> > Set rs = Nothing
>> > End Function
>> >
>> >

>>

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      3rd Mar 2008
Ok, I'll start testing. Thank you.

Geoff

"Ron de Bruin" wrote:

> Use the code in the links on top of the page
> You have much more control then and it is always working correct.
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Geoff" <(E-Mail Removed)> wrote in message news:38CB96C5-B07C-4167-B58C-(E-Mail Removed)...
> > Thank you for the quick response but that's a lot of code to go through
> > immediately and establish if it answers my question which is what happens if
> > the first column of source data is empty? Multiple workbooks comes after
> > I've settled this matter.
> >
> > Geoff
> >
> > "Ron de Bruin" wrote:
> >
> >> Try the examples for more workbooks on this page
> >> http://www.rondebruin.nl/ado.htm
> >>
> >> --
> >>
> >> Regards Ron de Bruin
> >> http://www.rondebruin.nl/tips.htm
> >>
> >>
> >> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
> >> > Hi
> >> > I would like to copy the first row of data from a number of wbooks using ADO
> >> > but for now the code below is just for 1 source wbook to the Log file.
> >> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
> >> > is installed. The code works fine providing there is data in cell A1. But
> >> > if the first cell containing data in the source file (test1) is say C1, then
> >> > data is still copied into the Log file starting at A1 whereas it ought to be
> >> > C1. Empty columns elsewhere in the source file row1 are copied correctly
> >> > How can I rectify this?
> >> > Is my approach the best way to go about the process for multiple wbooks?
> >> >
> >> > T.I.A.
> >> >
> >> > Geoff
> >> >
> >> > Option Explicit
> >> > Option Private Module
> >> >
> >> > Sub Test2()
> >> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
> >> > End Sub
> >> >
> >> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
> >> > As _ String)
> >> > Dim rs As ADODB.Recordset
> >> > Dim szConnect As String
> >> > Dim szSQL As String
> >> >
> >> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> >> > "Data Source=" & SourceFile & ";" & _
> >> > "Extended Properties=""Excel 8.0;HDR=No"";"
> >> > szSQL = "SELECT * FROM [Sheet1$];"
> >> > Set rs = New ADODB.Recordset
> >> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
> >> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
> >> > rs.Fields.Count
> >> > rs.Close
> >> > Set rs = Nothing
> >> > End Function
> >> >
> >> >
> >>

>

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      3rd Mar 2008
Sorry but it doesn't answer my question. After testing the great examples I
find that any blanks in the first row of the source are copied to the ADO
Tester file with the letter F and the column number. Blanks elsewhere in the
data are left blank.
The data files I have to deal with can have column headers at the bottom of
the data table, part way through or even no headers on some columns. This
means there can be blanks on the first row and I would need to ensure they
remain blank as I tidy the data up.
Which is where I started - how do I make sure if cell A1 is blank in the
source that it is replicated in the Log file?

Geoff

"Geoff" wrote:

> Ok, I'll start testing. Thank you.
>
> Geoff
>
> "Ron de Bruin" wrote:
>
> > Use the code in the links on top of the page
> > You have much more control then and it is always working correct.
> >
> >
> > --
> >
> > Regards Ron de Bruin
> > http://www.rondebruin.nl/tips.htm
> >
> >
> > "Geoff" <(E-Mail Removed)> wrote in message news:38CB96C5-B07C-4167-B58C-(E-Mail Removed)...
> > > Thank you for the quick response but that's a lot of code to go through
> > > immediately and establish if it answers my question which is what happens if
> > > the first column of source data is empty? Multiple workbooks comes after
> > > I've settled this matter.
> > >
> > > Geoff
> > >
> > > "Ron de Bruin" wrote:
> > >
> > >> Try the examples for more workbooks on this page
> > >> http://www.rondebruin.nl/ado.htm
> > >>
> > >> --
> > >>
> > >> Regards Ron de Bruin
> > >> http://www.rondebruin.nl/tips.htm
> > >>
> > >>
> > >> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
> > >> > Hi
> > >> > I would like to copy the first row of data from a number of wbooks using ADO
> > >> > but for now the code below is just for 1 source wbook to the Log file.
> > >> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
> > >> > is installed. The code works fine providing there is data in cell A1. But
> > >> > if the first cell containing data in the source file (test1) is say C1, then
> > >> > data is still copied into the Log file starting at A1 whereas it ought to be
> > >> > C1. Empty columns elsewhere in the source file row1 are copied correctly
> > >> > How can I rectify this?
> > >> > Is my approach the best way to go about the process for multiple wbooks?
> > >> >
> > >> > T.I.A.
> > >> >
> > >> > Geoff
> > >> >
> > >> > Option Explicit
> > >> > Option Private Module
> > >> >
> > >> > Sub Test2()
> > >> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
> > >> > End Sub
> > >> >
> > >> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
> > >> > As _ String)
> > >> > Dim rs As ADODB.Recordset
> > >> > Dim szConnect As String
> > >> > Dim szSQL As String
> > >> >
> > >> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> > >> > "Data Source=" & SourceFile & ";" & _
> > >> > "Extended Properties=""Excel 8.0;HDR=No"";"
> > >> > szSQL = "SELECT * FROM [Sheet1$];"
> > >> > Set rs = New ADODB.Recordset
> > >> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
> > >> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
> > >> > rs.Fields.Count
> > >> > rs.Close
> > >> > Set rs = Nothing
> > >> > End Function
> > >> >
> > >> >
> > >>

> >

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      3rd Mar 2008
Use the code in the links on top of the page
You have much more control then and it is always working correct.


See the pages below if you want to open the files with code and merge the data

Merge data from all workbooks in a folder(Dir)
http://www.rondebruin.nl/copy3.htm

Merge data from all workbooks in a folder(FSO)
http://www.rondebruin.nl/fso.htm

RDBMerge Add-in (very easy)
http://www.rondebruin.nl/merge.htm


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Geoff" <(E-Mail Removed)> wrote in message news:C65F3959-5D34-4D99-8447-(E-Mail Removed)...
> Sorry but it doesn't answer my question. After testing the great examples I
> find that any blanks in the first row of the source are copied to the ADO
> Tester file with the letter F and the column number. Blanks elsewhere in the
> data are left blank.
> The data files I have to deal with can have column headers at the bottom of
> the data table, part way through or even no headers on some columns. This
> means there can be blanks on the first row and I would need to ensure they
> remain blank as I tidy the data up.
> Which is where I started - how do I make sure if cell A1 is blank in the
> source that it is replicated in the Log file?
>
> Geoff
>
> "Geoff" wrote:
>
>> Ok, I'll start testing. Thank you.
>>
>> Geoff
>>
>> "Ron de Bruin" wrote:
>>
>> > Use the code in the links on top of the page
>> > You have much more control then and it is always working correct.
>> >
>> >
>> > --
>> >
>> > Regards Ron de Bruin
>> > http://www.rondebruin.nl/tips.htm
>> >
>> >
>> > "Geoff" <(E-Mail Removed)> wrote in message news:38CB96C5-B07C-4167-B58C-(E-Mail Removed)...
>> > > Thank you for the quick response but that's a lot of code to go through
>> > > immediately and establish if it answers my question which is what happens if
>> > > the first column of source data is empty? Multiple workbooks comes after
>> > > I've settled this matter.
>> > >
>> > > Geoff
>> > >
>> > > "Ron de Bruin" wrote:
>> > >
>> > >> Try the examples for more workbooks on this page
>> > >> http://www.rondebruin.nl/ado.htm
>> > >>
>> > >> --
>> > >>
>> > >> Regards Ron de Bruin
>> > >> http://www.rondebruin.nl/tips.htm
>> > >>
>> > >>
>> > >> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
>> > >> > Hi
>> > >> > I would like to copy the first row of data from a number of wbooks using ADO
>> > >> > but for now the code below is just for 1 source wbook to the Log file.
>> > >> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
>> > >> > is installed. The code works fine providing there is data in cell A1. But
>> > >> > if the first cell containing data in the source file (test1) is say C1, then
>> > >> > data is still copied into the Log file starting at A1 whereas it ought to be
>> > >> > C1. Empty columns elsewhere in the source file row1 are copied correctly
>> > >> > How can I rectify this?
>> > >> > Is my approach the best way to go about the process for multiple wbooks?
>> > >> >
>> > >> > T.I.A.
>> > >> >
>> > >> > Geoff
>> > >> >
>> > >> > Option Explicit
>> > >> > Option Private Module
>> > >> >
>> > >> > Sub Test2()
>> > >> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
>> > >> > End Sub
>> > >> >
>> > >> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
>> > >> > As _ String)
>> > >> > Dim rs As ADODB.Recordset
>> > >> > Dim szConnect As String
>> > >> > Dim szSQL As String
>> > >> >
>> > >> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
>> > >> > "Data Source=" & SourceFile & ";" & _
>> > >> > "Extended Properties=""Excel 8.0;HDR=No"";"
>> > >> > szSQL = "SELECT * FROM [Sheet1$];"
>> > >> > Set rs = New ADODB.Recordset
>> > >> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
>> > >> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
>> > >> > rs.Fields.Count
>> > >> > rs.Close
>> > >> > Set rs = Nothing
>> > >> > End Function
>> > >> >
>> > >> >
>> > >>
>> >

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      3rd Mar 2008
Apologies if I have not made myself clear. The issue at the moment is not to
do with multiple workbooks.

It is to sort out what happens if there is a gap in the headers.

In the examples supplied if there is a blank header then:
rsData.Fields(lCount).Name = the letter F plus the column number where the
blank occurred.

I cannot have F1 or F2 inserted in the log file, it must remain blank.

Geoff

"Ron de Bruin" wrote:

> Use the code in the links on top of the page
> You have much more control then and it is always working correct.
>
>
> See the pages below if you want to open the files with code and merge the data
>
> Merge data from all workbooks in a folder(Dir)
> http://www.rondebruin.nl/copy3.htm
>
> Merge data from all workbooks in a folder(FSO)
> http://www.rondebruin.nl/fso.htm
>
> RDBMerge Add-in (very easy)
> http://www.rondebruin.nl/merge.htm
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Geoff" <(E-Mail Removed)> wrote in message news:C65F3959-5D34-4D99-8447-(E-Mail Removed)...
> > Sorry but it doesn't answer my question. After testing the great examples I
> > find that any blanks in the first row of the source are copied to the ADO
> > Tester file with the letter F and the column number. Blanks elsewhere in the
> > data are left blank.
> > The data files I have to deal with can have column headers at the bottom of
> > the data table, part way through or even no headers on some columns. This
> > means there can be blanks on the first row and I would need to ensure they
> > remain blank as I tidy the data up.
> > Which is where I started - how do I make sure if cell A1 is blank in the
> > source that it is replicated in the Log file?
> >
> > Geoff
> >
> > "Geoff" wrote:
> >
> >> Ok, I'll start testing. Thank you.
> >>
> >> Geoff
> >>
> >> "Ron de Bruin" wrote:
> >>
> >> > Use the code in the links on top of the page
> >> > You have much more control then and it is always working correct.
> >> >
> >> >
> >> > --
> >> >
> >> > Regards Ron de Bruin
> >> > http://www.rondebruin.nl/tips.htm
> >> >
> >> >
> >> > "Geoff" <(E-Mail Removed)> wrote in message news:38CB96C5-B07C-4167-B58C-(E-Mail Removed)...
> >> > > Thank you for the quick response but that's a lot of code to go through
> >> > > immediately and establish if it answers my question which is what happens if
> >> > > the first column of source data is empty? Multiple workbooks comes after
> >> > > I've settled this matter.
> >> > >
> >> > > Geoff
> >> > >
> >> > > "Ron de Bruin" wrote:
> >> > >
> >> > >> Try the examples for more workbooks on this page
> >> > >> http://www.rondebruin.nl/ado.htm
> >> > >>
> >> > >> --
> >> > >>
> >> > >> Regards Ron de Bruin
> >> > >> http://www.rondebruin.nl/tips.htm
> >> > >>
> >> > >>
> >> > >> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
> >> > >> > Hi
> >> > >> > I would like to copy the first row of data from a number of wbooks using ADO
> >> > >> > but for now the code below is just for 1 source wbook to the Log file.
> >> > >> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
> >> > >> > is installed. The code works fine providing there is data in cell A1. But
> >> > >> > if the first cell containing data in the source file (test1) is say C1, then
> >> > >> > data is still copied into the Log file starting at A1 whereas it ought to be
> >> > >> > C1. Empty columns elsewhere in the source file row1 are copied correctly
> >> > >> > How can I rectify this?
> >> > >> > Is my approach the best way to go about the process for multiple wbooks?
> >> > >> >
> >> > >> > T.I.A.
> >> > >> >
> >> > >> > Geoff
> >> > >> >
> >> > >> > Option Explicit
> >> > >> > Option Private Module
> >> > >> >
> >> > >> > Sub Test2()
> >> > >> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
> >> > >> > End Sub
> >> > >> >
> >> > >> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
> >> > >> > As _ String)
> >> > >> > Dim rs As ADODB.Recordset
> >> > >> > Dim szConnect As String
> >> > >> > Dim szSQL As String
> >> > >> >
> >> > >> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> >> > >> > "Data Source=" & SourceFile & ";" & _
> >> > >> > "Extended Properties=""Excel 8.0;HDR=No"";"
> >> > >> > szSQL = "SELECT * FROM [Sheet1$];"
> >> > >> > Set rs = New ADODB.Recordset
> >> > >> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
> >> > >> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
> >> > >> > rs.Fields.Count
> >> > >> > rs.Close
> >> > >> > Set rs = Nothing
> >> > >> > End Function
> >> > >> >
> >> > >> >
> >> > >>
> >> >

>

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      3rd Mar 2008
For the purpose of copying the first row of data - including any gaps in the
source file I put this into your function code and it now permits gaps
without filling the cell with F and whatever colnum.

For lCount = 0 To rsData.Fields.Count - 1
If Not rsData.Fields(lCount).Name = "F" & 1 + lCount Then
TargetRange.Cells(1, 1 + lCount).Value = rsData.Fields(lCount).Name
Else
TargetRange.Cells(1, 1 + lCount).Value = ""
End If
Next lCount

It still does not answer my question which was how to use the
CopyFromRecordset method. It seems to me the recordset starts at the first
non blank column of source data which may not be column A.

But thank you for the suggestions I am sure the multiple wbook solutions you
provide will come in very useful.

Geoff

"Geoff" wrote:

> Apologies if I have not made myself clear. The issue at the moment is not to
> do with multiple workbooks.
>
> It is to sort out what happens if there is a gap in the headers.
>
> In the examples supplied if there is a blank header then:
> rsData.Fields(lCount).Name = the letter F plus the column number where the
> blank occurred.
>
> I cannot have F1 or F2 inserted in the log file, it must remain blank.
>
> Geoff
>
> "Ron de Bruin" wrote:
>
> > Use the code in the links on top of the page
> > You have much more control then and it is always working correct.
> >
> >
> > See the pages below if you want to open the files with code and merge the data
> >
> > Merge data from all workbooks in a folder(Dir)
> > http://www.rondebruin.nl/copy3.htm
> >
> > Merge data from all workbooks in a folder(FSO)
> > http://www.rondebruin.nl/fso.htm
> >
> > RDBMerge Add-in (very easy)
> > http://www.rondebruin.nl/merge.htm
> >
> >
> > --
> >
> > Regards Ron de Bruin
> > http://www.rondebruin.nl/tips.htm
> >
> >
> > "Geoff" <(E-Mail Removed)> wrote in message news:C65F3959-5D34-4D99-8447-(E-Mail Removed)...
> > > Sorry but it doesn't answer my question. After testing the great examples I
> > > find that any blanks in the first row of the source are copied to the ADO
> > > Tester file with the letter F and the column number. Blanks elsewhere in the
> > > data are left blank.
> > > The data files I have to deal with can have column headers at the bottom of
> > > the data table, part way through or even no headers on some columns. This
> > > means there can be blanks on the first row and I would need to ensure they
> > > remain blank as I tidy the data up.
> > > Which is where I started - how do I make sure if cell A1 is blank in the
> > > source that it is replicated in the Log file?
> > >
> > > Geoff
> > >
> > > "Geoff" wrote:
> > >
> > >> Ok, I'll start testing. Thank you.
> > >>
> > >> Geoff
> > >>
> > >> "Ron de Bruin" wrote:
> > >>
> > >> > Use the code in the links on top of the page
> > >> > You have much more control then and it is always working correct.
> > >> >
> > >> >
> > >> > --
> > >> >
> > >> > Regards Ron de Bruin
> > >> > http://www.rondebruin.nl/tips.htm
> > >> >
> > >> >
> > >> > "Geoff" <(E-Mail Removed)> wrote in message news:38CB96C5-B07C-4167-B58C-(E-Mail Removed)...
> > >> > > Thank you for the quick response but that's a lot of code to go through
> > >> > > immediately and establish if it answers my question which is what happens if
> > >> > > the first column of source data is empty? Multiple workbooks comes after
> > >> > > I've settled this matter.
> > >> > >
> > >> > > Geoff
> > >> > >
> > >> > > "Ron de Bruin" wrote:
> > >> > >
> > >> > >> Try the examples for more workbooks on this page
> > >> > >> http://www.rondebruin.nl/ado.htm
> > >> > >>
> > >> > >> --
> > >> > >>
> > >> > >> Regards Ron de Bruin
> > >> > >> http://www.rondebruin.nl/tips.htm
> > >> > >>
> > >> > >>
> > >> > >> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
> > >> > >> > Hi
> > >> > >> > I would like to copy the first row of data from a number of wbooks using ADO
> > >> > >> > but for now the code below is just for 1 source wbook to the Log file.
> > >> > >> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
> > >> > >> > is installed. The code works fine providing there is data in cell A1. But
> > >> > >> > if the first cell containing data in the source file (test1) is say C1, then
> > >> > >> > data is still copied into the Log file starting at A1 whereas it ought to be
> > >> > >> > C1. Empty columns elsewhere in the source file row1 are copied correctly
> > >> > >> > How can I rectify this?
> > >> > >> > Is my approach the best way to go about the process for multiple wbooks?
> > >> > >> >
> > >> > >> > T.I.A.
> > >> > >> >
> > >> > >> > Geoff
> > >> > >> >
> > >> > >> > Option Explicit
> > >> > >> > Option Private Module
> > >> > >> >
> > >> > >> > Sub Test2()
> > >> > >> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
> > >> > >> > End Sub
> > >> > >> >
> > >> > >> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
> > >> > >> > As _ String)
> > >> > >> > Dim rs As ADODB.Recordset
> > >> > >> > Dim szConnect As String
> > >> > >> > Dim szSQL As String
> > >> > >> >
> > >> > >> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> > >> > >> > "Data Source=" & SourceFile & ";" & _
> > >> > >> > "Extended Properties=""Excel 8.0;HDR=No"";"
> > >> > >> > szSQL = "SELECT * FROM [Sheet1$];"
> > >> > >> > Set rs = New ADODB.Recordset
> > >> > >> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
> > >> > >> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
> > >> > >> > rs.Fields.Count
> > >> > >> > rs.Close
> > >> > >> > Set rs = Nothing
> > >> > >> > End Function
> > >> > >> >
> > >> > >> >
> > >> > >>
> > >> >

> >

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      3rd Mar 2008
Forget ADO

Use the code that open the workbooks in the links I posted
You have full control then

http://www.rondebruin.nl/copy3.htm
Try example 2 to select the workbook you want


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Geoff" <(E-Mail Removed)> wrote in message news:EDF8B9F6-A5BE-4F97-9C18-(E-Mail Removed)...
> For the purpose of copying the first row of data - including any gaps in the
> source file I put this into your function code and it now permits gaps
> without filling the cell with F and whatever colnum.
>
> For lCount = 0 To rsData.Fields.Count - 1
> If Not rsData.Fields(lCount).Name = "F" & 1 + lCount Then
> TargetRange.Cells(1, 1 + lCount).Value = rsData.Fields(lCount).Name
> Else
> TargetRange.Cells(1, 1 + lCount).Value = ""
> End If
> Next lCount
>
> It still does not answer my question which was how to use the
> CopyFromRecordset method. It seems to me the recordset starts at the first
> non blank column of source data which may not be column A.
>
> But thank you for the suggestions I am sure the multiple wbook solutions you
> provide will come in very useful.
>
> Geoff
>
> "Geoff" wrote:
>
>> Apologies if I have not made myself clear. The issue at the moment is not to
>> do with multiple workbooks.
>>
>> It is to sort out what happens if there is a gap in the headers.
>>
>> In the examples supplied if there is a blank header then:
>> rsData.Fields(lCount).Name = the letter F plus the column number where the
>> blank occurred.
>>
>> I cannot have F1 or F2 inserted in the log file, it must remain blank.
>>
>> Geoff
>>
>> "Ron de Bruin" wrote:
>>
>> > Use the code in the links on top of the page
>> > You have much more control then and it is always working correct.
>> >
>> >
>> > See the pages below if you want to open the files with code and merge the data
>> >
>> > Merge data from all workbooks in a folder(Dir)
>> > http://www.rondebruin.nl/copy3.htm
>> >
>> > Merge data from all workbooks in a folder(FSO)
>> > http://www.rondebruin.nl/fso.htm
>> >
>> > RDBMerge Add-in (very easy)
>> > http://www.rondebruin.nl/merge.htm
>> >
>> >
>> > --
>> >
>> > Regards Ron de Bruin
>> > http://www.rondebruin.nl/tips.htm
>> >
>> >
>> > "Geoff" <(E-Mail Removed)> wrote in message news:C65F3959-5D34-4D99-8447-(E-Mail Removed)...
>> > > Sorry but it doesn't answer my question. After testing the great examples I
>> > > find that any blanks in the first row of the source are copied to the ADO
>> > > Tester file with the letter F and the column number. Blanks elsewhere in the
>> > > data are left blank.
>> > > The data files I have to deal with can have column headers at the bottom of
>> > > the data table, part way through or even no headers on some columns. This
>> > > means there can be blanks on the first row and I would need to ensure they
>> > > remain blank as I tidy the data up.
>> > > Which is where I started - how do I make sure if cell A1 is blank in the
>> > > source that it is replicated in the Log file?
>> > >
>> > > Geoff
>> > >
>> > > "Geoff" wrote:
>> > >
>> > >> Ok, I'll start testing. Thank you.
>> > >>
>> > >> Geoff
>> > >>
>> > >> "Ron de Bruin" wrote:
>> > >>
>> > >> > Use the code in the links on top of the page
>> > >> > You have much more control then and it is always working correct.
>> > >> >
>> > >> >
>> > >> > --
>> > >> >
>> > >> > Regards Ron de Bruin
>> > >> > http://www.rondebruin.nl/tips.htm
>> > >> >
>> > >> >
>> > >> > "Geoff" <(E-Mail Removed)> wrote in message news:38CB96C5-B07C-4167-B58C-(E-Mail Removed)...
>> > >> > > Thank you for the quick response but that's a lot of code to go through
>> > >> > > immediately and establish if it answers my question which is what happens if
>> > >> > > the first column of source data is empty? Multiple workbooks comes after
>> > >> > > I've settled this matter.
>> > >> > >
>> > >> > > Geoff
>> > >> > >
>> > >> > > "Ron de Bruin" wrote:
>> > >> > >
>> > >> > >> Try the examples for more workbooks on this page
>> > >> > >> http://www.rondebruin.nl/ado.htm
>> > >> > >>
>> > >> > >> --
>> > >> > >>
>> > >> > >> Regards Ron de Bruin
>> > >> > >> http://www.rondebruin.nl/tips.htm
>> > >> > >>
>> > >> > >>
>> > >> > >> "Geoff" <(E-Mail Removed)> wrote in message news:431948DF-0E9F-4F58-951B-(E-Mail Removed)...
>> > >> > >> > Hi
>> > >> > >> > I would like to copy the first row of data from a number of wbooks using ADO
>> > >> > >> > but for now the code below is just for 1 source wbook to the Log file.
>> > >> > >> > It copies row 1 data from Test1.xls in the "C" directory to wherever Log.xls
>> > >> > >> > is installed. The code works fine providing there is data in cell A1. But
>> > >> > >> > if the first cell containing data in the source file (test1) is say C1, then
>> > >> > >> > data is still copied into the Log file starting at A1 whereas it ought to be
>> > >> > >> > C1. Empty columns elsewhere in the source file row1 are copied correctly
>> > >> > >> > How can I rectify this?
>> > >> > >> > Is my approach the best way to go about the process for multiple wbooks?
>> > >> > >> >
>> > >> > >> > T.I.A.
>> > >> > >> >
>> > >> > >> > Geoff
>> > >> > >> >
>> > >> > >> > Option Explicit
>> > >> > >> > Option Private Module
>> > >> > >> >
>> > >> > >> > Sub Test2()
>> > >> > >> > GetData "C:\Test1.xls", "Sheet1", "A1:IV1"
>> > >> > >> > End Sub
>> > >> > >> >
>> > >> > >> > Function GetData(SourceFile As Variant, SourceSheet As String, sourceRange
>> > >> > >> > As _ String)
>> > >> > >> > Dim rs As ADODB.Recordset
>> > >> > >> > Dim szConnect As String
>> > >> > >> > Dim szSQL As String
>> > >> > >> >
>> > >> > >> > szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
>> > >> > >> > "Data Source=" & SourceFile & ";" & _
>> > >> > >> > "Extended Properties=""Excel 8.0;HDR=No"";"
>> > >> > >> > szSQL = "SELECT * FROM [Sheet1$];"
>> > >> > >> > Set rs = New ADODB.Recordset
>> > >> > >> > rs.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
>> > >> > >> > Workbooks("Log.xls").Sheets(1).Cells(1, 1).CopyFromRecordset rs, 1, _
>> > >> > >> > rs.Fields.Count
>> > >> > >> > rs.Close
>> > >> > >> > Set rs = Nothing
>> > >> > >> > End Function
>> > >> > >> >
>> > >> > >> >
>> > >> > >>
>> > >> >
>> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying the Hyperlink function result without copying the actual formula mcheng Microsoft Excel Worksheet Functions 2 9th Jun 2007 02:43 AM
Copying the filtered data to clipboard is copying non-visible rows =?Utf-8?B?U2VldGhhUmFtYW4=?= Microsoft Excel Crashes 10 12th Jul 2006 09:39 PM
Creating WBooks =?Utf-8?B?R2VvZmY=?= Microsoft Excel Programming 5 9th Jun 2006 10:14 AM
RePost - Creating WBooks =?Utf-8?B?R2VvZmY=?= Microsoft Excel Programming 4 9th Jun 2006 09:20 AM
show list of wbooks present into a dir sal21 Microsoft Excel Programming 1 22nd Sep 2005 01:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:26 AM.