简体   繁体   中英

VBA Excel For Each Loop

The nested loops should take a point and find its description and write it into another column. The problem I have is only the value from B20 is written into C10:C20 . If I change the fourth line to b.value , it works correctly for the output but in the wrong column so I believe it's a looping issue but I don't see the solution.

For Each b In Worksheets("Device").Range("B10:B20").Cells
    For Each c In Worksheets("Device").Range("C10:C20").Cells
        Set pt = srv.PIPoints(b.Value)
        c.Value = pt.PointAttributes.Item("descriptor")
    Next
Next

Try to use this one instead:

For Each b in WorkSheets("Device").Range("B10:B20").Cells   
    Set pt = srv.PIPoints(b.Value)
    b.Offset(,1).Value = pt.PointAttributes.Item("descriptor")
Next

where b.Offset(,1) gives you cell one column to the right from b , ie if b points to B11 then b.Offset(,1) points to C11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM