tags:

views:

33

answers:

1

I have Book1.csv and Book2.xlsx. Book1.csv has many columns with data. Each column has unique title in first ROW. I need to find column with title “Processor Time” and copy all available data in this column to column1 in Book2.xlsx using VBscript. Please help.

A: 

You can use ADO to get the column:

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;"
strcon = strcon & "Extended Properties=""Text;FMT=Delimited;HDR=Yes;IMEX=1"";"

cn.Open strcon

strSQL = "Select [Processor Time] From [Book1.csv]"

rs.Open strSQL, cn

MsgBox rs.GetString

You can use automation with Excel:

Set xl = CreateObject("Excel.Application")

You can write to Excel from a recordset with:

xl.Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs

You can post back if you have problems putting the bits together.

EDIT re Comment

Try switching your code around a little:

Set xl = CreateObject("Excel.Application") 
xl.Visible = True 
Set objWorkbook1=xl.Workbooks.Open("C:\Docs\book2.xlsx") 

Set cn = CreateObject("ADODB.Connection") 
Set rs = CreateObject("ADODB.Recordset") 

strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;" 
strcon = strcon & "Extended Properties=""Text;FMT=Delimited;HDR=Yes;IMEX=1"";" 

cn.Open strcon 
strSQL = "Select [Processor Time] From [Book1.csv]" 
rs.Open strSQL, cn 

objWorkbook1.Worksheets("Sheet1").Cells(2, 1).CopyFromRecordset rs 
Remou
I am sorry but I do not know how to put these bits together. My files are located in c:\temp. Please help.
Naim
How far have you got?
Remou
I get MsgBox with data which is under Processor Time title. I am not sure how to copy this data to Book2.xlsx column A.
Naim
see code below.
Naim
This opens book2.xlsx but it's empty. It did not copy column data.
Naim
Set cn = CreateObject("ADODB.Connection") Set rs = CreateObject("ADODB.Recordset") strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;" strcon = strcon FMT=Delimited;HDR=Yes;IMEX=1"";" cn.Open strcon strSQL = "Select [Processor Time] From [Book1.csv]" rs.Open strSQL, cn MsgBox rs.GetString Set xl = CreateObject("Excel.Application") xl.Visible = TrueSet objWorkbook1= xl.Workbooks.Open("C:\Docs\book2.xlsx")objWorkbook1.Worksheets("Sheet1").Cells(2, 1).CopyFromRecordset rs
Naim
I think it is to do with timing. I have added a note.
Remou
OK. So I have it working. In this script, we only search for "Processor Time" and copied that column. How do I search for more that one strings and copy all those columns. For example, I want to search for tiles "Processor time", "Memory" and "Page Faults" and copy all these columns and paste in book2.xlsx column A, B and C.
Naim
You can list the columns you want: `Select [Processor Time], [Memory], [Page Faults] From [Book1.csv]` You can select all : `Select * From [Book1.csv]` , though it is generally better to list the column names. You can even use WHERE statements : `Select [Processor Time], [Memory], [Page Faults] From [Book1.csv] Where [Page Faults] > 6`
Remou
Thanks. Now, I have a title name "(PDH-CSV 4.0) (Eastern Daylight Time)(240)". When I replace "Processor Time" in above code with new title I get an error "Invalid bracketing of name "(PDH-CSV 4.0) (Eastern Daylight Time)(240)". If there anyway to fix this?
Naim
I can think of three possibilities. 1. Set HDR=No and use F1,F2, F3, ...,Fn for column names. 2. Open the file using the FileSystemObject and change the headers. 3. Use Excel automation for the Book1.csv. Options 1 and 3 are probably the best bet.
Remou