Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS & Excel: Copy single field to new field for all records

Status
Not open for further replies.

eao

MIS
Nov 14, 2001
104
US
I want to copy the value of cell A1 into column F for each record that exists in Sheet1. I have found some code that allows me to open the workbook I want:

Code:
Option Explicit
Const vbNormal = 1         ' window style

DIM objXL, objWb, objR, objTab     ' Excel object variables
DIM Text, tmp, i, j, file, name

' here you may set the name of the file to be imported 
file = "foo.txt"    ' must be located in the script folder

' create an Excel object reference
Set objXL = WScript.CreateObject ("Excel.Application")

' set the Excel window properties (not absolutely necessary)
objXL.WindowState = vbNormal ' Normal
objXL.Visible = true         ' do not show window

' Create new Workbook (needed for import the CSV file=
Set objWb = objXl.WorkBooks.Add 

' Get the first loaded worksheet object of the current workbook
Set objWb = objXL.ActiveWorkBook.WorkSheets(1)

I also will want to delete row 1 once the copy is complete. Can anyone point me in the right direction? Thanks.
 
This should put the string "foo" in cell A1, correct?

Code:
ObjXL.ActiveSheet.Cells(1,1).Value = "foo"

So could I do something like this to set one cell equal to the value of another?

Code:
ObjXL.ActiveSheet.Cells(6,1).Value = ObjXL.ActiveSheet.Cells(1,1).Value

How would I do that for each row?
 
Maybe I could find the number of rows, and then loop through and copy the cell to each row like this:

Code:
DIM iRowNumber
set xlSht = xlApp.activesheet

iRowNumber=1
while xlSht.Cells(iRowNumber, 1) <> ""
iRowNumber = iRowNumber + 1
wend

while iRowNumber > 0
ObjXL.ActiveSheet.Cells(6,iRowNumber).Value = ObjXL.ActiveSheet.Cells(1,1).Value
iRowNumber = iRowNumber -1
wend

Does anyone think that's got a chance?
 
Hello eao,

What you have found by yourself is making a good lot of sense and certainly you can assign value to cell like that. Test it out with a testing workbook.

regards - tsuji
 
Thanks tsuji,

Sometimes I just need think out loud to solve a problem.
 
You may test and copy in a single loop:
iRowNumber=1
While xlSht.Cells(iRowNumber, 1) <> ""
xlSht.Cells(6,iRowNumber).Value = xlSht.Cells(1,1).Value
iRowNumber = iRowNumber + 1
Wend

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top