Solved

Excel, add row, shift all values down - Please Help!!!

Posted on 2007-11-15
4
739 Views
Last Modified: 2008-02-01
I am taking a .txt file delimiting it and exporting to excel. I need to add a row (column names) from a different spreadsheet to where my exported spreadsheet exists.  
thanks in advance
strExcelPath = "C:\myfiles\header.xls"
 

Const ForReading = 1

Const ForWriting = 2
 

'Added to save changes without being prompted by Excel

Const xlSaveChanges = 1
 

Set objFSO = CreateObject("Scripting.FileSystemObject")
 

Set objFile = objFSO.OpenTextFile("C:\myfiles\test.txt", ForReading)

strContents = objFile.ReadAll

objFile.Close
 

strContents = Replace(strContents, "|", vbTab)
 

Set objFile = objFSO.OpenTextFile("C:\myfiles\test.txt", ForWriting)

objFile.Write strContents

objFile.Close
 

Set objExcel = CreateObject("Excel.Application")

'Add row here???

'Shift all other values down

Set objWorkbook = objExcel.Workbooks.Open("C:\myfiles\test.txt")

objExcel.ActiveWorkbook.cells.addrow("1,1")

objExcel.ActiveWorkbook.SaveAs strExcelPath

objExcel.ActiveWorkbook.Close xlSaveChanges

objExcel.Application.Quit

WScript.Echo "The Upload is Complete"

Open in new window

0
Comment
Question by:gabulish
  • 2
4 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 20300506
Instead of:
objExcel.ActiveWorkbook.cells.addrow("1,1")


Try:
objExcel.ActiveWorkbook.ActiveWorksheet.Rows(1).Insert

0
 
LVL 49

Accepted Solution

by:
DanRollins earned 150 total points
ID: 20300567
One option would be to insert the line when you write the file (rather than after importing into Excel).  That might look something like this:
Set objFile = objFSO.OpenTextFile("C:\myfiles\test.txt", ForReading)

strContents = objFile.ReadAll

objFile.Close

 

strContents = Replace(strContents, "|", vbTab)

 

Set objFile = objFSO.OpenTextFile("C:\myfiles\test.txt", ForWriting)

 

' REM added this line --------------------------

objFile.Write "clm1" +vbTab+ "clm2" +vbTab+ "clm3" +vbCr

 

objFile.Write strContents

objFile.Close

Open in new window

0
 

Author Comment

by:gabulish
ID: 20300680
DanRollins,

I like your approach. Could I open another WorkBook and grab a row from a Worksheet and paste it into the original Workbook, Worksheet?

thanks much,

gabulish
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 20301551
I suppose so.  You would need to access functions of your objExcel object and extract the desired text from the specidied cells.  I have little experience with the Excel object model.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now