Hello Experts,
I am new to VB Script. Looking for help on following issue mentioned as step 2,3. Any help is appreciated.
Thanks,
SK
Steps:
1. I am opening excel files in some folders to refresh as files have queries in it. (no issue – done)
2. I need to copy certain data values from the files that i opened in step 1. Basically, I need just two column data A2 and F2
3. I have a master file that has some data already in it in “sheet1”. Now, I have to append copied data values in step 2 to end of this master file sheet1 plus hardcoding some cell values. For example:
In Master file append to end of sheet1:
column A = copied A2 (from step 2) and format the value that is coming as “Sat, 2012-07-06” to
“7/6/2012”. So, column A value will be=7/6/2012
Column B = Hardcoded value “abc”
Column C= Hardcoded value “efg”
Column D= Hardcoded value “0”
Column E= copied F2 (from step 2)
4. Automatically email the master excel (this is for later)
Below is my attempt:
option explicit
dim objFSO,objStartFolder,colFiles,objFolder,objFile, objSubFolder, myFiles,Subfolder
Dim oExcel,oWorkbook
Set oExcel = CreateObject("Excel.Application")
' Disable Excel UI elements
oExcel.Visible = True
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Work"
Set objFolder = objFSO.GetFolder(objStartFolder)
ShowSubfolders objFolder
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Set oWorkbook = oExcel.Workbooks.Open(Subfolder.Path+"\"+objFile.Name)
oWorkbook.RefreshAll
oWorkbook.Save
UpdateMaster(oWorkbook)
Next
ShowSubFolders Subfolder
Next
End Sub
Sub UpdateMaster(sourceWB)
dim sourceSheet
dim masterSheet
Set sourceSheet = sourceWB.Worksheets("InstallData")
‘copy only A2 and F2 column data
sourceSheet.Range("A2").Copy
sourceSheet.Range("F2").Copy
Dim appexcel, masterWb,rngMaster
Set appexcel = WScript.CreateObject("Excel.Application")
'appexcel.Visible = True
Set masterWb=appexcel.Workbooks.Open("C:\Work\ master\master.xlsx")
Set masterSheet = masterWb.Worksheets("Sheet1")
masterSheet.Activate
'Need to get to end of master
'Append copied data and hardcoded data
‘Save the sheet/workbook
Set masterWb = Nothing
appexcel.Quit
Set appexcel = Nothing
end sub
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
Bookmarks