【问题标题】:Combobox not adding defined named range组合框不添加定义的命名范围
【发布时间】:2017-06-13 05:31:50
【问题描述】:

我在 sheet1 中设置了两个组合框。我需要将工作表列表添加到组合框一,它工作正常。我需要将 sheet2 的第一列添加到组合框二,第一个单元格作为名称(称为“名称”)。此代码适用于我的用户窗体,使用 Me 而不是 Sheet1,但使用其中任何一个都不适用于我。

我收到“对象不支持此属性或方法”错误。

谢谢,

Private Sub Workbook_Open()

    Dim refSheet As Worksheet
    Set refSheet = ActiveWorkbook.Sheets(2)

    Dim oSheet As Excel.Worksheet

    For Each oSheet In ActiveWorkbook.Sheets

        Sheet1.ComboBox1.AddItem oSheet.Name

    Next oSheet

    Dim lastrow As Long

    lastrow = refSheet.Cells(Rows.Count, 1).End(xlUp).Row

    With refSheet.Columns(1)
        Range(Cells(1, 1), Cells(lastrow, 1)).Select
        Selection.CreateNames Top:=True
    End With

    Sheet1.ComboBox2.RowSource = "Name"

End Sub

【问题讨论】:

  • 有时,sheet1 与 sheet(1) 不同。您可以在 vbe 中签入项目资源管理器。也许您的写作需要 sheet(1).shapes("combobox1") 或 oleobject 或其他任何东西......

标签: excel combobox vba


【解决方案1】:

参考表格:

lastrow = refSheet.Cells(Rows.Count, 1).End(xlUp).Row

应该是:

lastrow = refSheet.Cells(refSheet.Rows.Count, 1).End(xlUp).Row

当你在这里使用“With”时:

With refSheet.Columns(1)
    Range(Cells(1, 1), Cells(lastrow, 1)).Select
    Selection.CreateNames Top:=True
End With

你应该将它与点一起使用:

With refSheet.Columns(1)
    .Range(.Cells(1, 1), .Cells(lastrow, 1)).Select
    Selection.CreateNames Top:=True
End With

(注意范围和单元格之前的点) 在“With”块中不使用点是指 ActiveSheet

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多