简体   繁体   中英

Python generator function - is there an excel equivalent?

I'm looking to run through a large list of numbers in Excel, and need the programme to be as accessible as possible for other people.

I'd like to keep the spreadsheet Excel only (possibly VBA), but is there an equivalent of Python's generator, or yield functions available in Excel?

ie

gen = (x for x in range(10))

As mentioned in the comments it would be good if you could exactly describe what you are trying to do in Excel .

Excel can perform array formulas. Within those array formulas we can use ROW or COLUMN function to get arrays of successive integers.

For example:

{=SUM(ROW(1:10))}

inputted as an array formula with [Ctrl]+[Shift]+[Enter] will calculate the sum of the integers from 1 to 10.

To input an array formula, input the formula into the cell without the curly brackests and then press [Ctrl]+[Shift]+[Enter] to confirm. The curly brackets will then appear automatically.

Or with VBA:

Sub test()

 Dim gen As Variant

 gen = [{0,1,2,3,4,5,6,7,8,9}] 
 ' gen is now an Array Variant(1 to 10) containing integers 0 to 9

 gen = [ROW(1:10)-1] 
 ' gen is now an Array Variant(1 to 10, 1 to 1) containing integers 0 to 9

 For Each i In gen

  MsgBox i

 Next

 Erase gen ' free the memory used by gen

End Sub

will loop from 0 to 9.

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