excel 2007方法对象的oleobjects失败

时间:2015-01-21 21:46:51

标签: excel vba runtime-error

我在单独的工作表上设置了带有验证列表的发票,列出了我们销售的所有零件。我将组合框放在发票上并将它们链接到验证列表并包含代码,这样当双击框时,它将在键入时使用验证列表自动完成框。我还包括代码,以便当这个发票在一天结束时关闭,然后在第二天重新打开,或者当按下快捷键时,它将清除内容并更改发票号。

有时我需要保存一张发票,以便稍后添加或更改。因此,我复制该工作表并使用客户名重命名。这已经好一年多了。但是上周,当我点击复制的工作表上的任何单元格时,它有一个运行时错误1004对象“_Worksheet”的方法“OLEObjects”失败。然后组合框不起作用。但它只在复制的工作表上执行。原始工作表工作正常。有什么建议?以下是使用的代码:

    '==========================
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
      Cancel As Boolean) 
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim wsList As Worksheet
Set ws = ActiveSheet
Set wsList = Sheets("ValidationLists")
Cancel = True
Set cboTemp = ws.OLEObjects("Parts")
On Error Resume Nex
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
  'show the combobox with the list
  .Visible = True
  .Left = Target.Left
  .Top = Target.Top
  .Width = Target.Width + 5
  .Height = Target.Height + 5
  .ListFillRange = str
  .LinkedCell = Target.Address
End With
cboTemp.Activate
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
'=========================================
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Application.EnableEvents = False
Application.ScreenUpdating = True
If Application.CutCopyMode Then
'allow copying and pasting on the worksheet
GoTo errHandler
End If
Set cboTemp = ws.OLEObjects("Parts")
On Error Resume Next
With cboTemp
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'====================================
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
Private Sub Parts_KeyDown(ByVal _
    KeyCode As MSForms.ReturnInteger, _
    ByVal Shift As Integer)
Select Case KeyCode
    Case 9 'Tab
        ActiveCell.Offset(0, 1).Activate
    Case 13 'Enter
        ActiveCell.Offset(1, 0).Activate
    Case Else
        'do nothing
End Select
End Sub

设置cboTemp = ws.OLEObjects(“Parts”)就是问题所在。它出现两次并在两者上都被标记。

0 个答案:

没有答案