Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-15
4
Medium Priority
?
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 450 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

598 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