根据组合框选择动态添加列值

时间:2018-11-13 15:56:48

标签: excel vba excel-vba

我需要你的帮助。看来我用代码编写的内容无法完成我在这里想要做的事情。

目标是要有2个用户窗体组合框,其中一个(地板)值会手动添加一次[3,4,5],其他组合框(办公室)会根据选择动态添加值在楼层选择框中完成。

例如,假设我在楼层组合框中选择值[3],则办公室组合框将包含以下值:

A-01
A-02
A-03
A-04
A-05
A-06
A-07
A-08

我以为这段代码可以用,但是不能用

'Cells(row, col)

Private Sub floor_Change()
    lRow = Sheets("Office Spaces").UsedRange.Rows.Count

    With Sheets("Office Spaces")
        For i = 2 To lRow
            If .Cells(i, 1).Value = UserForm1.floor.Value Then
                UserForm1.office.AddItem .Cells(i, 2).Value
            End If
        Next i
    End With
End Sub

这是我的excel工作表中的数据:

enter image description here

1 个答案:

答案 0 :(得分:0)

'Cells(row, col)

Private Sub floor56_Change()

UserForm1.office.Clear

Dim sh
Dim rw

Set sh = Sheets("Office Spaces")

For Each rw In sh.Rows

  If sh.Cells(rw.row, 1).Text = UserForm1.floor.Value Then

    UserForm1.office.AddItem (sh.Cells(rw.row, 2).Value)

  End If

Next rw

End Sub

Private Sub floor_Change()

    If UserForm1.floor.Value <> "" Then

        UserForm1.office.Clear

        Dim ws
        Set ws = ThisWorkbook.Worksheets("Office Spaces")

        Dim rng
        Set rng = ws.Range("A:A")

        For Each cell In rng

            If cell.Text = UserForm1.floor.Value Then

                UserForm1.office.AddItem (cell.Offset(0, 1).Value)

            End If

        Next cell

    End If

End Sub