PDA

View Full Version : [SOLVED:] Help with loop



john3j
05-14-2015, 10:08 AM
I am trying to parse xml files either one at a time or in bulk, which p45cal helped me do in another project. The problem is that I am trying to dump the output from each .nessus file into a new row on the worksheet. When I run the single file multiple times, or process in bulk, I end up getting the appropriate number of rows populated, but each of the rows overwrites with the latest .nessus file's output. So, each row ends up with the same data in it. Can anyone help me out?

Here is my code:


Sub populateRows(HostName, Manufacturer, ModelNum, BiosVer, SerialNum, IPAddress, OSID)
Dim j As Long
For j = 2 To Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Hardware List").Cells(j, "a").Value = HostName
Sheets("Hardware List").Cells(j, "b").Value = Manufacturer
Sheets("Hardware List").Cells(j, "c").Value = ModelNum
Sheets("Hardware List").Cells(j, "d").Value = BiosVer
Sheets("Hardware List").Cells(j, "h").Value = SerialNum
Sheets("Hardware List").Cells(j, "i").Value = IPAddress
Sheets("Hardware List").Cells(j, "j").Value = OSID
Next j
End Sub

13395

Any help would be appreciated.

SamT
05-14-2015, 11:29 AM
Running that sub on the workshee4t "HardwareLiist" results in a loop

For J = 2 to 2
'Get data, put on row 2
Next j
Assuming you already had 10 rows of data the code would say

On data filled rows 2 to 11
'overwrite with new data.

Try this

Sub populateRows(HostName, Manufacturer, ModelNum, BiosVer, SerialNum, IPAddress, OSID)
'Puts data in first empty row of sheet.

With Sheets("Hardware List").Rows(Cells(Rows.Count, 1).End(xlUp).Row + 1)
'the above is only one row, so counting Cells will be counting Columns.
.Cells(1).Value = HostName
.Cells(2).Value = Manufacturer
.Cells(3).Value = ModelNum
.Cells(4).Value = BiosVer
.Cells(5).Value = SerialNum
.Cells(6).Value = IPAddress
.Cells(7).Value = OSID
End With
End Sub
Instead of using .Cells(1) in the above, you could use .Cells(1, "A"), which says "The Cell in Row 1 of the 1 row Range, Column "A"." Note that using .Cells("A") would be an undocumented feature. which should not be trusted.






With Sheets("Hardware List").Rows(Cells(Rows.Count, 1).End(xlUp).Row + 1)
is the same as


Dim NR As Long 'NR= Next Row
With Sheets("Hardware List")
NR = .Cells(Rows.Count, 1).End(xlUp).Row + 1
With .Rows(NR)
blah, blah, blah,
End With
End With

john3j
05-14-2015, 12:19 PM
Wow. Thank you for the very detailed response. Your suggestion worked perfectly and I learned something in the process. Thank you again!