简体   繁体   English

VBA 将增量值添加到 combobox

[英]VBA add incremented values into combobox

| | am wondering how to populate items list for a combobox from value to value by increment of value?我想知道如何通过值的增量从一个值到另一个值填充 combobox 的项目列表? The way I do it now is by hand via:我现在这样做的方式是通过以下方式手动操作:

LowerFilmWidth_ComboBox.AddItem "300"

In this example I am trying to add values from 300 to 650 by increment of 1在此示例中,我尝试将 300 到 650 的值以 1 递增

Could someone, please, share a code for doing that?有人可以分享这样做的代码吗?

Please, try the next code:请尝试下一个代码:

Sub AddIncrementedNrCombo()
  Dim i As Long, arr(350)
  For i = 300 To 650
        arr(i - 300) = i
  Next i

  LowerFilmWidth_ComboBox.list = arr
End Sub

Edited :编辑

A version allowing incrementing with a different increment:允许以不同增量递增的版本:

Sub AddIncrementedNrCombo()
  Dim i As Long, arr(350), k As Long, inc As Long
  
  inc = 10 'it can be calculated
  For i = 300 To 650 Step inc
        arr(k) = i: k = k + 1
  Next i
  
  ReDim Preserve arr(k - 1)
  LowerFilmWidth_ComboBox.list = arr
End Sub

Edit2 :编辑2

Sub AddIncrementedNrComboVar()
  Dim i As Long, arr(), k As Long, mN As Long, mX As Long, inc As Long
  
  mN = 300 'min
  mX = 650 'max
  inc = 10
  ReDim arr(mX - nm)
  
  For i = mN To mX Step inc
        arr(k) = i: k = k + 1
  Next i
  
  ReDim Preserve arr(k - 1)
  LowerFilmWidth_ComboBox.list = arr
End Sub

If you want to be fancy, you could use something like:如果你想花哨,你可以使用类似的东西:

LowerFilmWidth_ComboBox.List = [Row(300:650)]

But for future reference you should know about the For loop where you could Step through in a specified interval (1 by default)但为了将来参考,您应该了解For循环,您可以在其中以指定的时间间隔Step执行(默认为 1)

For x = 300 To 650 'Step 1
    LowerFilmWidth_ComboBox.AddItem x
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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