激活其他工作簿并在该工作簿中执行function

我有'Workbook1'有命令button来执行某些操作。 在此工作簿中单击button时,它将从Outlook下载不同的工作簿,并使用variables名称(“Workbook2”)打开该工作簿。 在此之后,我想在该工作簿中设置一个filter。 但我无法做到这一点。 我得到“对象variables或块variables未设置”错误。 以下是我的代码。

Dim EXCELApplication As Object Dim DefPath As Variant Dim wb As Workbook Dim wbName As String Dim col2 As Long Dim colNameF As Long Dim colNameF1 As Long Dim colNameF2 As Long ' Other Relevant Code Present Here' DoEvents Set EXCELApplication = CreateObject("Excel.Application") EXCELApplication.Workbooks.Open (DefPath & strExt & ".xlsb") EXCELApplication.Visible = True EXCELApplication.Sheets("Release Level View").Activate colNameF = Range("A8:DD8").Find(What:="Teams", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column colNameF1 = Range("A8:DD8").Find(What:="Items", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column colNameF2 = Range("A8:DD8").Find(What:="Domain", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column ActiveSheet.Range("$A$8:$DD$9999").AutoFilter Field:=colNameF, Criteria1:="ST Test", Operator:=xlOr, Criteria2:="" ActiveSheet.Range("$A$8:$DD$9999").AutoFilter Field:=colNameF1, Criteria1:="Variance", Operator:=xlOr, Criteria2:="" ActiveSheet.Range("$A$8:$DD$9999").AutoFilter Field:=colNameF2, Criteria1:="9S", Operator:=xlOr, Criteria2:="" 

我在这个特定的行中遇到错误。

 colNameF = Range("A8:DD8").Find(What:="Teams", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column 

即使我使用ActiveSheet.Range ..我仍然得到相同的错误。 有人能告诉我这是什么问题吗?


谢谢BK201,即使我使用Set,我仍然得到相同的错误。 以下是您理解的完整代码。

 With targetSht Set aCell1 = EXCELApplication.Range("A8:DD8").Find(What:="Feb", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False) If Not aCell1 Is Nothing Then col2 = aCell1.Column SV1 = Split(Cells(col2).Address, "$")(1) lRow1 = .Range(SV1 & .Rows.Count).End(xlUp).Row End If colNameF = .Range("A8:DD8").Find(What:="Teams", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column colNameF1 = .Range("A8:DD8").Find(What:="Items", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column colNameF2 = .Range("A8:DD8").Find(What:="Domain", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column .Range("$A$8:$DD$9999").AutoFilter Field:=colNameF, Criteria1:="ST Test", Operator:=xlOr, Criteria2:="" .Range("$A$8:$DD$9999").AutoFilter Field:=colNameF1, Criteria1:="Variance", Operator:=xlOr, Criteria2:="" .Range("$A$8:$DD$9999").AutoFilter Field:=colNameF2, Criteria1:="9S", Operator:=xlOr, Criteria2:="" .Cells(lRow1 + 1, SV1).Select Selection.NumberFormat = "0" SumV1 = SV1 & "9" SumW1 = SV1 & lRow1 .Cells(lRow1 + 1, SV1).Formula = "=SUBTOTAL(9," & SumV1 & ":" & SumW1 & ")" .Cells(lRow1 + 1, SV1).Select Selection.Copy End With Windows("DS.xlsx").Activate Set FindV = Range("A1:Z100").Find(What:="Dec Rel", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False) FindV.Offset(0, 4).NumberFormat = "0" FindV.Offset(0, 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 

无论遇到“单元格”方法,我都会遇到同样的错误。

两件事情:

(1)使用ActiveSheet或直接到Range意味着你从调用macros的工作簿中运行。 由于您打开了一本新书,因此您有两个工作簿处于活动状态,但是您的代码设置方式是针对当前工作簿的。

(2)这个代码块可能会导致一些问题:

 Set EXCELApplication = CreateObject("Excel.Application") EXCELApplication.Workbooks.Open (DefPath & strExt & ".xlsb") EXCELApplication.Visible = True EXCELApplication.Sheets("Release Level View").Activate 

注意: EXCELApplication.Sheets 。 如果有效,它仍然是不好的编码。 改变它是这样的:

 Dim targetBk as Workbook Set xlApp = CreateObject("Excel.Application") With xlApp Set targetBk = .Workbooks.Open (DefPath & strExt & ".xlsb") .Visible = True End With targetBk.Sheets("Release Level View").Activate 

即使如此, Activate及其同类也是不好的。 最好更明确一点,

 Dim targetBk as Workbook, targetSht As Worksheet Set xlApp = CreateObject("Excel.Application") With xlApp Set targetBk = .Workbooks.Open (DefPath & strExt & ".xlsb") .Visible = True End With Set targetSht = targetBk.Sheets("Release Level View") With targetSht colNameF = .Range("A8:DD8").Find(What:="Teams").Column colNameF1 = .Range("A8:DD8").Find(What:="Items").Column colNameF2 = .Range("A8:DD8").Find(What:="Domain").Column .Range("$A$8:$DD$9999").AutoFilter Field:=colNameF, Criteria1:="ST Test", Operator:=xlOr, Criteria2:="" .Range("$A$8:$DD$9999").AutoFilter Field:=colNameF1, Criteria1:="Variance", Operator:=xlOr, Criteria2:="" .Range("$A$8:$DD$9999").AutoFilter Field:=colNameF2, Criteria1:="9S", Operator:=xlOr, Criteria2:="" End With 

希望这可以帮助!

编辑:关于你的评论,永远记住,默认返回一个Range 。 这意味着,如果将其分配给一个没有任何其他属性的variables,那么就是将一个范围分配给一个variables。 不用说,这需要Set来正确地发生。 见下文:

 Sub Test() Set aCell1 = Range("A1:DD8").Find(What:="Feb") col2 = aCell1.Column SV1 = Split(Cells(col2).Address, "$")(1) lRow1 = Range(SV1 & Rows.Count).End(xlUp).Row ActiveSheet.Cells(lRow1 + 1, SV1).NumberFormat = "0" End Sub 

上面的工作现在,因为col2可以正确识别一个aCell1为一个Range 。 此外,您的格式化行( ...NumberFormat = "0" )是正确的。

让我们知道这是否有帮助。

编辑2:您的Cells使用应始终合格。 如果您使用的是With targetSht ,那么SV1 = Split(.Cells(col2).Address, "$")(1) 。 注意到. 那里有.Cells 。 无论如何,尝试改变下面的代码块我修改:

 With targetSht Set aCell1 = .Range("A8:DD8").Find(What:="Feb") If Not aCell1 Is Nothing Then col2 = aCell1.Column SV1 = Split(.Cells(col2).Address, "$")(1) lRow1 = .Range(SV1 & .Rows.Count).End(xlUp).Row End If colNameF = .Range("A8:DD8").Find(What:="Teams").Column colNameF1 = .Range("A8:DD8").Find(What:="Items").Column colNameF2 = .Range("A8:DD8").Find(What:="Domain").Column With .Range("$A$8:$DD$9999") .AutoFilterMode = False .AutoFilter Field:=colNameF, Criteria1:="ST Test", Operator:=xlOr, Criteria2:="" .AutoFilter Field:=colNameF1, Criteria1:="Variance", Operator:=xlOr, Criteria2:="" .AutoFilter Field:=colNameF2, Criteria1:="9S", Operator:=xlOr, Criteria2:="" End With .Cells(lRow1 + 1, SV1).NumberFormat = "0" SumV1 = SV1 & "9" SumW1 = SV1 & lRow1 .Cells(lRow1 + 1, SV1).Formula = "=SUBTOTAL(9," & SumV1 & ":" & SumW1 & ")" .Cells(lRow1 + 1, SV1).Select Selection.Copy End With 

让我们知道这是否有帮助。

当您尝试在excel实例中运行find method时,您在单独的Excel实例中打开工作簿。 尝试这个:

 colNameF = EXCELApplication.Workbooks(DefPath & strExt & ".xlsb"). _ Sheets("Release Level View"). _ Range("A8:DD8").Find(What:="Teams", LookIn:=xlValues, _ LookAt:=xlWhole, MatchCase:=False, _ SearchFormat:=False).Column 

如果您将Object Variable设置为Sheets("Release Level View") ,并在代码中进一步使用它将会更好。