Custom Search

Monday, March 30, 2009

Changing the Data in Excel sheets from all cloumns and rows

Option Explicit
Rem We use "Option Explicit" to help us check for coding mistakes
Rem the Excel Application

Dim objExcel
Rem the path to the excel file
Dim excelPath
Rem how many worksheets are in the current excel file
Dim worksheetCount
Dim counter
Rem the worksheet we are currently getting data from
Dim currentWorkSheet
Rem the number of columns in the current worksheet that have data in them
Dim usedColumnsCount
Rem the number of rows in the current worksheet that have data in them
Dim usedRowsCount
Dim row
Dim column
Rem the topmost row in the current worksheet that has data in it
Dim top
Rem the leftmost row in the current worksheet that has data in it
Dim left
Dim Cells
Rem the current row and column of the current worksheet we are reading
Dim curCol
Dim curRow
Rem the value of the current row and column of the current worksheet we are reading
Dim word
Rem find the value like "Testing\ATS" with in the Cell Data
Dim mypos
Rem split the Cell data
Dim myarray
Rem Find the Local Machine Name
Dim WshNetwork
Dim Computername
set WshNetwork= WScript.CreateObject("Wscript.Network")
Computername = WshNetwork.Computername
Rem where is the Excel file located?
excelPath = "C:\Documents and Settings\krao\Desktop\Inputs.xls"
'WScript.Echo "Reading Data from " & excelPath
Dim oFSO , objFile

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = oFSO.GetFile(excelPath)
' Checks for file attributes. This is a check for file being readonly. As the latests is taken from VSTS it will be marked as readonly. This code will uncheck the Readonly property
If objFile.Attributes AND 1 Then
objFile.Attributes = objFile.Attributes - 1
End If
Rem Create an invisible version of Excel
Set objExcel = CreateObject("Excel.Application")
Rem don't display any messages about documents needing to be converted
Rem from old Excel file formats
objExcel.DisplayAlerts = 0
Rem open the excel document
objExcel.Workbooks.open excelPath ', True , true
Rem How many worksheets are in this Excel documents
workSheetCount = objExcel.Worksheets.Count
'WScript.Echo "We have " & workSheetCount & " worksheets"
Rem Loop through each worksheet
For counter = 1 to workSheetCount
'WScript.Echo "Reading data from worksheet " & counter & vbCRLF
Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets(counter)
Rem how many columns are used in the current worksheet
usedColumnsCount = currentWorkSheet.UsedRange.Columns.Count
Rem how many rows are used in the current worksheet
usedRowsCount = currentWorkSheet.UsedRange.Rows.Count
Rem What is the topmost row in the spreadsheet that has data in it
top = currentWorksheet.UsedRange.Row
Rem What is the leftmost column in the spreadsheet that has data in it
left = currentWorksheet.UsedRange.Column
Set Cells = currentWorksheet.Cells
Rem Loop through each row in the worksheet
For row = 1 to (usedRowsCount-1)
Rem Loop through each column in the worksheet
For column = 0 to usedColumnsCount-1
Rem only look at rows that are in the "used" range
curRow = row+top
Rem only look at columns that are in the "used" range
curCol = column+left
Rem get the value/word that is in the cell
word = Cells(curRow,curCol).Value
Rem find the value like "Testing\ATS" with in the Cell Data
mypos=inStrRev(word,"Testing\ATS")
If mypos>=1 Then
Rem split the Cell data
myarray=Split(word,"ATS",-1,1)
If myarray(0)="D:\Testing\" Then
Rem Change the Data "D:\Testing" to "C:\Testing"
myarray(0)="C:\Testing\"
Rem Add Total Cell Data
word=myarray(0)&"ATS"&myarray(1)
Rem Enter the Modified Data in Cell
Cells(curRow,curCol).Value=word
End If
End If
Rem Change the Cell Data(machine name) other then Local machine name
If Ucase(word)= "ALP-QASIM13-2K3" Then
Cells(curRow,curCol).Value=Computername
End If
Rem display the column on the screen
'WScript.Echo (word)
Next
Next
Rem We are done with the current worksheet, release the memory
Set currentWorkSheet = Nothing
Next
objExcel.workbooks(1).save
objExcel.Workbooks(1).Close
objExcel.Quit
Set currentWorkSheet = Nothing
Rem We are done with the Excel object, release it from memory
Set objExcel = Nothing

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.