激活不同的工作簿并在该工作簿中执行功能

时间:2013-12-05 07:50:12

标签: excel vba excel-vba

我有'Workbook1',它有命令按钮来执行某些操作。在此工作簿中单击按钮时,它会从Outlook下载不同的工作簿,并使用变量名称打开该工作簿,即“Workbook2”。在此之后,我想在该工作簿中设置一个过滤器。但我无法做到这一点。我正在 '对象变量或使用块变量未设置' 错误。以下是我的代码。

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

无论遇到'cells'方法,我都会遇到同样的错误。

2 个答案:

答案 0 :(得分:1)

两件事:

(1)使用ActiveSheet或直接转到Range表示您正在从正在调用宏的工作簿中运行。由于您打开了一本新书,因此您有两个工作簿处于活动状态,但您正在以设置代码的方式定位当前工作簿。

(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

希望这有帮助!

编辑:关于您的评论,请务必记住Find默认返回Range。这意味着如果将其分配给没有任何其他属性的变量,则可以为变量分配范围。不用说,这需要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可以aCell1Range正确识别为具有.Column属性的...NumberFormat = "0"。此外,您的格式行(Cells)是正确的。

如果有帮助,请告诉我们。

EDIT2:您对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 。无论如何,尝试将代码块改为我的修改:

{{1}}

如果有帮助,请告诉我们。

答案 1 :(得分:0)

当您尝试在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")并在代码中进一步使用它会更好。