我有“ 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”方法,到处都会出现相同的错误。
两件事情:
(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
可以正常工作,因为可以正确识别aCell1
为Range
具有.Column
属性。另外,您的格式行(...NumberFormat = "0"
)是正确的。
让我们知道是否有帮助。
EDIT2:您对的使用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
让我们知道是否有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句