Results 1 to 2 of 2

how to copy data from excel columns in a sheet and append them at end in a different sheet

Threaded View

  1. #1
    Registered User
    Join Date
    07-17-2012
    Location
    SF
    MS-Off Ver
    Excel 2007
    Posts
    1

    how to copy data from excel columns in a sheet and append them at end in a different sheet

    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
    Last edited by Leith Ross; 07-17-2012 at 10:46 PM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1