组合框不返回所选的listindex值

时间:2017-01-25 20:36:18

标签: excel vba excel-vba combobox

我正在创建一个工作簿,跟踪每月可用的租金。它分为12张,每月一张。每张表的前三列跟踪住宿类型,卧室数量以及租赁价格中包含的内容。其中的概念是会有一个下拉组合框,允许用户使用点击选项填写,而不是输入内容以减少输入错误。

我设置了一个固定数组,其内容根据活动单元格所在的列而变化,然后将数组分配给组合框。代码位于组合框代码下的Sheet1模块中,ThisWorkbook模块在SheetSelectionChange下调用它,以避免重复每个工作表中的代码。

标准模块使数组公开

所有12个组合框共享相同的名称cboOptions,并且无论选择何种工作表,它们都会正确填充。我的问题是没有任何组合框返回所做选择的listindex值,无论代码告诉它如何。我一直在测试看到的位置值是否与所选位置的值相对应,但我无法建立模式。我想清除变量和数组,认为这可能是代码的混乱,但似乎没有效果。我已经在这个问题上阅读了我能说的内容,但我对可能出现的问题提出了自己的看法......提前谢谢你们!

Sheet1模块中的代码:

    Private Sub cboOptions_Change()

Erase myarray()
cboOptions.Visible = True
cboOptions.Enabled = True
cboOptions.Clear

n = ActiveCell.Row
 If n >= 3 And n < 10000 Then

    If ActiveSheet.Range(ActiveCell.Address).Address = Range("A" & n).Address Then

        myarray(1) = "Apartment"
        myarray(2) = "Room"
        myarray(3) = "Townhouse"
        myarray(4) = "House"


    ElseIf ActiveSheet.Range(ActiveCell.Address).Address = Range("B" & n).Address Then

        myarray(1) = "1"
        myarray(2) = "2"
        myarray(3) = "3"
        myarray(4) = "4"
        myarray(5) = "5"


    ElseIf ActiveSheet.Range(ActiveCell.Address).Address = Range("C" & n).Address Then

        myarray(1) = "Heat & Water"
        myarray(2) = "All-inclusive"

    Else

        cboOptions.Enabled = False
        cboOptions.Visible = False

    End If

  End If
'ActiveSheet.cboOptions.ListIndex = 0


'Dim x As Long
'MsgBox ActiveSheet.Name

With ActiveSheet
.cboOptions.Left = .Range(ActiveCell.Address).Left
.cboOptions.Top = .Range(ActiveCell.Address).Top

.cboOptions.List = myarray()


With .cboOptions
'the problem is that x needs to get assigned a value from the combo box before it continues to execute

    x = .List(.ListIndex)
    'MsgBox x
    End With
    .Range(ActiveCell.Address) = x 'myarray(x)
.Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
x = 0

Erase myarray()

End With

End Sub

ThisWorkbook中的代码:

Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)

Application.Run "Sheet1.cboOptions_Change"

End Sub

Module1中的代码:

Option Explicit
Public myarray(0 To 5) As String

1 个答案:

答案 0 :(得分:1)

问题的本质似乎是在一个组合框中使用多个数组会分解listindex值的计算方式。我将代码分解为其组件功能,以查看问题是否仍然存在

1)制作一个新文件并将代码放入Sheet1中 2)为每种输入制作单独的固定阵列 3)为每种输入创建一个单独的例程

在每个例程的开头使用ON ERROR RESUME NEXT会忽略错误并且代码正常工作。或者,在给出整数变量的中断处,组合框的listindex值允许用户在继续之前做出选择并为整数变量赋值。否则,其默认值为-1并返回错误;使用.list(.listindex)没有任何区别,建议代码需要等待用户输入(使用除Change之外的组合框事件?)。

可能只需要为每列建立一个单独的组合框。无论如何,对于单个工作表,下面的代码是上面的粘土版本,如果应用于工作簿中的每个工作表模块,它将完成工作:

Sub monthnames()
'add month names to the first cell of each sheet

Dim n As Integer
'Sheets(1).Activate
For n = 1 To 12
    Sheets.Add After:=ActiveSheet
    ThisWorkbook.Sheets(n).Cells(1, 1) = MonthName(n)
    Next
End Sub

Private Sub cboOptions_Change()

Dim myarray(1 To 4) As String
Dim myarray2(1 To 5) As String
Dim myarray3(1 To 2) As String

cboOptions.Enabled = True
cboOptions.Visible = True

Dim n As Integer

n = ActiveCell.Row
 If n >= 3 And n < 10000 Then

    If Range(ActiveCell.Address).Address = Range("A" & n).Address Then
        myarray(1) = "Apartment"
        myarray(2) = "Room"
        myarray(3) = "Townhouse"
        myarray(4) = "House"
        cboOptions.List = myarray()
        inputdata myarray(), n

    ElseIf Range(ActiveCell.Address).Address = Range("B" & n).Address Then

        myarray2(1) = "1"
        myarray2(2) = "2"
        myarray2(3) = "3"
        myarray2(4) = "4"
        myarray2(5) = "5"
        cboOptions.List = myarray2()
        inputdata2 myarray2(), n

   ElseIf Range(ActiveCell.Address).Address = Range("C" & n).Address Then

        myarray3(1) = "Heat & Water"
        myarray3(2) = "All-inclusive"
        cboOptions.List = myarray3()
        inputdata3 myarray3(), n
    Else

        cboOptions.Enabled = False
        cboOptions.Visible = False

    End If

  End If


End Sub


Sub inputdata(myarray, n) 'myarray3, )

On Error Resume Next
Dim x As Integer

    cboOptions.Left = Range(ActiveCell.Address).Left
    cboOptions.Top = Range(ActiveCell.Address).Top
    Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18

    If Range(ActiveCell.Address).Address = Range("A" & n).Address Then

    x = cboOptions.ListIndex + 1

    Range(ActiveCell.Address) = myarray(x)

Else

    Exit Sub

End If

End Sub

Sub inputdata2(myarray2, n)
On Error Resume Next
Dim y As Integer

    cboOptions.Left = Range(ActiveCell.Address).Left
    cboOptions.Top = Range(ActiveCell.Address).Top
    Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18



    If Range(ActiveCell.Address).Address = Range("B" & n).Address Then

y = cboOptions.ListIndex + 1

    Range(ActiveCell.Address) = myarray2(y)

Else

    Exit Sub

End If

End Sub

Sub inputdata3(myarray3, n)
On Error Resume Next
Dim z As Integer

    cboOptions.Left = Range(ActiveCell.Address).Left
    cboOptions.Top = Range(ActiveCell.Address).Top
    Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18

If Range(ActiveCell.Address).Address = Range("C" & n).Address Then

z = cboOptions.ListIndex + 1

    Range(ActiveCell.Address) = myarray3(z)

Else

    Exit Sub

End If

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call cboOptions_Change
End Sub