While...Wend Loops

While...Wend loops are included in VBA for backward compatibility. In the VBA help file, Microsoft suggests that Do...Loops are more flexible. However, because you might encounter While...Wend loops in code written by others, here is a quick example. In this loop, the first line is always While condition. The last line of the loop is always Wend. Note that there is no Exit While statement. In general, these loops are okay, but the Do...Loop construct is more robust and flexible. Because it offers either the While or Until qualifier, this qualifier can be at the beginning or end of the loop, and there is the possibility to exit a Do loop early:

1 Read a text file, adding the amounts Open "C:\Invoice.txt" For Input As #1 TotalSales = 0 While Not EOF(1)

Line Input #1, Data TotalSales = TotalSales + Data


MsgBox "Total Sales=" & TotalSales Close #1

0 0

Post a comment