【问题标题】:run-time error '-2147221080 (800401a8)': Automation error运行时错误“-2147221080 (800401a8)”:自动化错误
【发布时间】:2018-11-05 19:12:28
【问题描述】:

我在尝试运行此宏时收到上述错误消息: (对不起尺寸和凌乱的状态) *

Private Sub CommandButton1_Click()

' Button to select one client from "Search Result" and fill their information in "Client Details"

'On Error GoTo CloseDataPool

' Make sure the Client Number cell is empty

If Range("B2") = "" Then
MsgBox ("Please enter valid client number")
Exit Sub
End If

' Give row value to Client Number
Search_Result = 4 + Range("B2")
If Cells(Search_Result, 1) <> Range("B2") Then
MsgBox ("Please enter valid client number")
Exit Sub
End If

' Find client in Data Pool via Broker Reference, then find their row number

Workbooks.Open "C:\Users\*\Dropbox\Shared Folder AT TH DH\Savant\*\Data Pool.xlsx"

Dim RC As Workbook
Dim DPW As Workbook
Dim DP As Worksheet
Dim SR As Worksheet
Dim CD As Worksheet
Dim PFDP As Worksheet
Set DPW = Workbooks("Data Pool")
Set DP = DPW.Worksheets("Data Pool")
Set RC = Workbooks("*")
Set SR = RC.Worksheets("Search Results")
Set CD = RC.Worksheets("Client Details")
Set PFDP = DPW.Worksheets("Prospect Fleet Data Pool")
Set PLDP = DPW.Worksheets("Prospect Liability Data Pool")

' Protect workbook and worksheets
CD.Protect Password:="*", UserInterfaceOnly:=True
SR.Protect Password:="*", UserInterfaceOnly:=True
DP.Protect Password:="*", UserInterfaceOnly:=True
PFDP.Protect Password:="*", UserInterfaceOnly:=True
PLDP.Protect Password:="*", UserInterfaceOnly:=True
RC.Protect Password:="*", Structure:=True
DPW.Protect Password:="*", Structure:=True

Search_Result = SR.Range("B2") + 4
x = DP.Cells(Rows.count, 1).End(xlUp).Row + 1
For Each Rowcheck In DP.Range("B2:B" & x)
If Rowcheck = SR.Range("B" & Search_Result) Then
y = Rowcheck.Row

CD.Range("E16") = DP.Cells(y, 1) 'Company Name
CD.Range("F38") = DP.Cells(y, 4) 'User added by
CD.Range("L38") = DP.Cells(y, 5) 'Date added on
End If
Next Rowcheck

Search_Result = CD.Range("F8")

Polcol = 2
Polrow = 45
x = PFDP.Cells(Rows.count, 1).End(xlUp).Row + 1
For Each Rowcheck In PFDP.Range("A2:A" & x)
If Rowcheck = Search_Result Then
y = Rowcheck.Row
CD.Cells(Polrow, Polcol) = Polrow - 44
CD.Cells(Polrow, Polcol + 2) = PFDP.Cells(y, 3)
Polrow = Polrow + 1
End If
Next Rowcheck

x = PLDP.Cells(Rows.count, 1).End(xlUp).Row + 1
For Each Rowcheck In PLDP.Range("A2:A" & x)
If Rowcheck = Search_Result Then
y = Rowcheck.Row
CD.Cells(Polrow, Polcol) = Polrow - 44
CD.Cells(Polrow, Polcol + 2) = PLDP.Cells(y, 3)
Polrow = Polrow + 1
End If
Next Rowcheck

'Add to history log
Set HLD = DPW.Worksheets("History Log")
HLD.Protect Password:="*", UserInterfaceOnly:=True
HLDR = HLD.Cells(Rows.count, 1).End(xlUp).Row + 1
HLD.Cells(HLDR, 1) = CD.Range("F8")

GoTo EndSub
CloseDataPool:
MsgBox ("An error has occurred")
EndSub:
Workbooks("Data Pool.xlsx").Save
Workbooks("Data Pool.xlsx").Close

End Sub

显示错误的行是:

Set HLD = DPW.Worksheets("History Log")

我试图将这条线移到我设置其他工作表的起点附近。我也将保护线移动到起点附近。当我这样做时,错误再次发生,但在下一行:

HLDR = HLD.Cells(Rows.count, 1).End(xlUp).Row + 1

我还在上面添加了一行来打开数据池工作簿,因为在谷歌搜索时,我发现在关闭的工作簿中查找项目可能会发生错误。但是,错误仍然存​​在。

我环顾四周,找不到解决方案。有人可以帮助我理解这个错误吗?

【问题讨论】:

  • 你能做一个minimal reproducible example,确保错误发生,只使用大约6-7行代码吗?
  • 我尝试将代码缩短到 6-7 行,以便宏仅适用于 HLD 变量。问题不再出现。 Set DPW = etc. Set HLD = Etc. HLD.Protect... 这一切正常。

标签: excel vba


【解决方案1】:

您发布的代码过多,无法获得量身定制的答案。相反,我将尝试以更一般的方式解释这个问题。像这样的行是您的问题的原因:

Workbooks.Open "C:\Users\*\Dropbox\Shared Folder AT TH DH\Savant\*\Data Pool.xlsx"
...
...
Workbooks("Data Pool.xlsx").Save

改为这样做:

Dim DataPool as Workbook
Set DataPool = Workbooks.Open("...\Data Pool.xlsx")
...
...
DataPool.Save

对该对象的所有其他引用都应该通过对象变量,而不是类。

【讨论】:

  • 感谢您的回复。我为冗长的代码道歉。我已经删除了一半不会以任何方式影响问题的行。我没有足够的知识来进一步减少它。我还进行了测试,以确保问题在缩短的代码中再次出现,并且确实如此。感谢您对改进我的代码的建议。遗憾的是,在这种情况下,错误仍然存​​在。
  • 只是为了确保 - 我上面的建议适用于该模式的所有出现。此外,像Set DPW = Workbooks("Data Pool") 这样的行也有问题。 Set DPW = Workbooks.Open("...\x.xlsx") 这里更好
  • 我已将所有内容更改为您建议的方法。起初它不起作用,但是在移动东西之后它突然开始起作用了。我不完全确定为什么,但我会接受的!再次感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 2014-01-14
  • 2013-10-23
  • 1970-01-01
相关资源
最近更新 更多