组合框与按钮结合使用

时间:2014-05-25 11:35:06

标签: excel-vba vba excel

我有一个组合框;进行选择时,使用该值填充链接的单元格。我想重复这个过程,但希望每次都直接填充下面的单元格。我曾想过使用一个按钮,但我不确定如何创建宏来实现这一点。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

“正下方的单元格” - 我假设您要将链接的单元格更改为下一行?
这有效;代码被评论。

Option Explicit

Dim insw& ' global needed because changing the row calls the _Change Sub.
' This is an "in-process" switch.

Sub combobox1Reset() ' reset to B5
  ComboBox1.Clear
  ComboBox1.AddItem "One"
  ComboBox1.AddItem "Two"
  ComboBox1.AddItem "Three"
  insw = 1
  ComboBox1.LinkedCell = Cells(5, 2).Address ' B5
  insw = 0
End Sub

Private Sub ComboBox1_Change()
  Dim iRow&, iCol&, s1$, zCell As Range
  If insw Then Exit Sub ' skip row change
  s1 = ComboBox1.LinkedCell ' old addr
  Set zCell = Range(s1) ' get row, col
  iRow = zCell.Row + 1 ' increment
  iCol = zCell.Column
  insw = 1 ' skip row change
  ComboBox1.LinkedCell = Cells(iRow, iCol).Address
  insw = 0
End Sub