Consulting

Results 1 to 3 of 3

Thread: Help with loop

  1. #1
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Indiana
    Posts
    113
    Location

    Help with loop

    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
    HardwareList.xlsm

    Any help would be appreciated.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    Last edited by SamT; 05-14-2015 at 11:59 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Indiana
    Posts
    113
    Location
    Wow. Thank you for the very detailed response. Your suggestion worked perfectly and I learned something in the process. Thank you again!

Posting Permissions

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