Combobox不添加定义的命名范围
我有两个combobox设置在Sheet1中。 我需要将工作表的列表添加到combobox,它工作正常。 我需要将sheet2的第一列添加到第一个单元格作为名称(称为“名称”)的combobox二。 此代码适用于我的用户窗体,与我而不是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
请参阅表格:
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