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)
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>
|
|
|
|
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)
Just build a single string in the loop then write that string out to excel after the loop.
|
|
|
|
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)
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".
|
|
|
|