Appending data to a cell in excel using vbscript

Author Message
MHHSGuru82

  • Total Posts : 1
  • Scores: 0
  • Reward points : 0
  • Joined: 11/28/2011
  • Status: offline
Appending data to a cell in excel using vbscript Monday, November 28, 2011 4:38 AM (permalink)
0
I am quering WMI for a list of logical disk objects using vbscript. Thus far the script works perfect except when I added a line to write the data to a cell in Excel. The problem is that it's in a For Each loop (which enumerates each logical disk object), the returned data is succesfully returned and written to the correct cell in Excel the only problem is that it is overwriting the previous object so at the end of the loop I am only left with the last disk object that was enumerated.
 
 ' k is equal to k + 1 so that for each computer object the next cell contains the correct data.   For Each objItem in colDiskItems ' Query WMI for and enumerate logical disks Set colDiskItems = objWMIService.ExecQuery("Select from Win32_LogicalDisk") ' Calculate the total size iof the disk in GB totalDiskSize = (objItem.Size) / (1024^3) ' Write the results to Excel objSheet.Cells(k,10).Value = "Name: " & objItem.Name & _ CHR(10) & "DeviceID: " & objItem.DeviceID & _ CHR(10) & "Disk Size: " & totalDiskSize & " (GB)" Next 

<message edited by MHHSGuru82 on Monday, November 28, 2011 4:39 AM>
 
#1
    ebgreen

    • Total Posts : 8219
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Appending data to a cell in excel using vbscript Monday, November 28, 2011 4:39 AM (permalink)
    0
    Just build a single string in the loop then write that string out to excel after the loop.
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      59cobalt

      • Total Posts : 969
      • Scores: 91
      • Reward points : 0
      • Joined: 7/17/2011
      • Status: offline
      Re:Appending data to a cell in excel using vbscript Monday, November 28, 2011 6:02 AM (permalink)
      0
      MHHSGuru82
      ' k is equal to k + 1 so that for each computer object the next cell contains the correct data.
      For Each objItem in colDiskItems
       ' Query WMI for and enumerate logical disks
       Set colDiskItems = objWMIService.ExecQuery("Select from Win32_LogicalDisk")
       ' Calculate the total size iof the disk in GB
       totalDiskSize = (objItem.Size) / (1024^3)
       ' Write the results to Excel
       objSheet.Cells(k,10).Value = "Name: " & objItem.Name & _
       CHR(10) & "DeviceID: " & objItem.DeviceID & _
       CHR(10) & "Disk Size: " & totalDiskSize & " (GB)"
      Next
      Unless the forum software totally messed up the order of your code lines, your code snippet doesn't make any sense. The WMI query belongs outside the loop, otherwise you'd be resetting colDiskItems with every loop cycle. However, the main problem is that you never increment k, so you're always writing to the same cell. k never "is equal to k + 1".
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9