【发布时间】:2020-08-06 12:54:39
【问题描述】:
我明白了尼克的建议,以下是我得到的错误编号和描述:
'-2147417848 (80010108)' 自动化错误调用的对象有 与客户断开连接
我调试时突出显示的代码行是:
.Rows(Lst).Insert Shift:=xlDown
我以为我在这个或另一个论坛上的某个地方看到了取消注册然后重新注册特定文件,但是当我遇到那个时我在家,并且不想在我的笔记本电脑上尝试它,因为一切已经 100% 工作了。
再次感谢任何帮助。我周日离开两周,我真的需要在离开之前完成这项工作。大多数为我工作的人都不是 excel 大师,需要所有按钮/功能正常工作,因为他们无法排除故障和/或解决问题。
我仍然在常规模块中使用以下代码,而下面的下一组代码在其中一个工作表模块中。
Sub add_InvRow()
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
switch = "off"
With ThisWorkbook
Dim wb As Excel.Workbook, Lst As Long
Set wb = Application.ThisWorkbook
Dim ws As Worksheet, sw As Worksheet, os As Worksheet
Set ws = ActiveSheet: Set sw = Application.Sheets(Sheet1.Name): Set os = Application.Sheets(Sheet4.Name)
With ws
Lst = ActiveCell.Row
End With
If ws.CodeName = "Sheet3" Then
With os
.Rows(213).Copy
End With
With ws
.Rows(Lst).Insert Shift:=xlDown
Application.CutCopyMode = False
venTabForm.Show
End With
End If
If ws.CodeName = "Sheet23" Then
With sw
.Rows(135).Copy
End With
With ws
.Rows(Lst).Insert Shift:=xlDown
Application.CutCopyMode = False
cItemForm.Show
End With
End If
If ws.CodeName = "Sheet25" Then
With sw
.Rows(105).Copy
End With
With ws
.Rows(Lst).Insert Shift:=xlDown
Application.CutCopyMode = False
coInvForm.Show
End With
End If
If ws.CodeName = "Sheet28" Then
With sw
.Rows(100).Copy
End With
With ws
.Rows(Lst).Insert Shift:=xlDown
Application.CutCopyMode = False
kInvForm.Show
End With
End If
If ws.CodeName = "Sheet27" Then
With sw
.Rows(130).Copy
End With
With ws
.Rows(Lst).Insert Shift:=xlDown
Application.CutCopyMode = False
ItemForm.Show
End With
End If
If ws.CodeName = "Sheet22" Then
With sw
.Rows(120).Copy
End With
With ws
.Rows(Lst).Insert Shift:=xlDown
Application.CutCopyMode = False
caInvForm.Show
End With
End If
Set ws = Nothing: Set sw = Nothing: Set os = Nothing: Set wb = Nothing
End With
switch = "on"
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
此代码位于具有命令按钮的工作表之一上,该按钮调用上述代码。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If switch = "off" Then Exit Sub
If Target.Address = "$H$1" Then
Call findItem
Exit Sub
End If
If Application.Intersect(Target, Me.Range("P:P")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
If Target.Cells.Value = 0 Or Target.Cells.Value = "" Then Exit Sub
Dim wb As Workbook, ws As Worksheet, iNUM As String, kitSHT As Worksheet, ksRNG As Range, kITEM As Range, kbCELL As Range
Dim iNAME As String, catSHT As Worksheet, csRNG As Range, cbCELL As Range, cITEM As Range
Dim logCELL As Range
Set wb = ThisWorkbook: Set ws = wb.Sheets(Sheet27.Name): Set kitSHT = wb.Sheets(Sheet28.Name): Set catSHT = wb.Sheets(Sheet22.Name)
Set ksRNG = kitSHT.Range("C5:C1100"): Set kbCELL = ksRNG.Cells(5, 3)
Set csRNG = catSHT.Range("C6:C400"): Set cbCELL = csRNG.Cells(6, 3)
If (Not (Application.Intersect(Target, Me.Range("A:P")) Is Nothing)) And (Target.Cells.Count = 1) And (Target.Column = 16) Then
If Target.Value = 0 Then Exit Sub
iNUM = Target.Offset(, -12).Value
iNAME = Target.Offset(, -10).Value
If kitSHT.Cells.Find(What:=iNUM, After:=kbCELL, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) Is Nothing And _
catSHT.Cells.Find(What:=iNUM, After:=cbCELL, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) Is Nothing Then
MsgBox iNUM & "-" & iNAME & "" & " is not currently listed on" & " " & kitSHT.Name & " " & "or" & " " & catSHT.Name & vbNewLine & vbNewLine & _
"Please add" & " " & iNUM & "-" & iNAME & "" & " to" & " " & kitSHT.Name & " " & _
"or" & " " & catSHT.Name & " " & "and corresponding count sheets", vbInformation
Set wb = Nothing: Set ws = Nothing: Set kbCELL = Nothing
Set ksRNG = Nothing: Set kitSHT = Nothing: Set cbCELL = Nothing: Set catSHT = Nothing: Set csRNG = Nothing
Exit Sub
Else
If Target.Value = 0 Then Exit Sub
premNUM = iNUM
pFORM.Show
End If
End If
Set wb = Nothing: Set ws = Nothing: Set kbCELL = Nothing
Set ksRNG = Nothing: Set kitSHT = Nothing: Set cbCELL = Nothing: Set catSHT = Nothing: Set csRNG = Nothing
Set ksRNG = Nothing: Set kitSHT = Nothing: Set cbCELL = Nothing: Set catSHT = Nothing: Set csRNG = Nothing
End Sub
【问题讨论】:
-
第一次尝试:最好使用
Application.EnableEvents = False来防止工作表更改...请尝试并判断错误是否仍然弹出...也尝试避免select(如果在非活动工作表中使用它会搞砸)。像.Rows(Lst).Select和Selection.EntireRow.Insert Shift:=xlDown将是.Rows(Lst).Insert Shift:=xlDown:) -
在 Worksheet_Change 事件中,您同时拥有
If Target.Address = "$H$1"和If Target.Address <> "H1"(即不一致的地址格式 - 两者都应该是 $H$1)。在此处使用文本比较不是一个好习惯 - 请改用 Application.Intersect,如下所示:If Not (Application.Intersect(Target, Me.Range("H1")) Is Nothing) Then -
@barrowc - 我同意您的建议,但您提出的测试与 OP 使用的测试不同...
-
不需要在过程结束时将对象变量设置为 Nothing。
-
@Tim Williams - 公平点 -
If (Not (Application.Intersect(Target, Me.Range("H1")) Is Nothing)) And (Target.Count = 1) Then将是相同的测试