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
2010-10-28 19:33:21
I am sorry but I do not know how to put these bits together. My files are located in c:\temp. Please help.
Naim
2010-10-28 20:04:26
How far have you got?
Remou
2010-10-28 20:19:52
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
2010-10-29 14:13:10
see code below.
Naim
2010-10-29 14:43:57
This opens book2.xlsx but it's empty. It did not copy column data.
Naim
2010-10-29 14:45:09
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
2010-10-29 14:46:17
I think it is to do with timing. I have added a note.
Remou
2010-10-29 15:29:31
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
2010-10-29 16:14:11
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
2010-10-29 16:24:44
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
2010-10-29 16:48:35
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
2010-10-29 18:47:09