我一直在努力工作一天半的代码。我有一个超过50列18000行的电子表格。我已经能够基于H(OpsCol)列中的空白单元格来确定由“ AllEntRg”定义的A列中较小范围的单元格。我陷入循环的底部。对于EntityRg,我将遍历“ AllEntRg”中的每个单元格,如果未在BudWb Wk4中定义的Range CCRg中找到它,那么我想创建所有这些单元格的范围。下一个选项CostCRg,我想为CCrg中找到的所有单元格定义一个范围。
我已经通过选择单个单元格对其进行了测试,它提供了我想要的结果,但是当我将其放入循环中时,我得到了以下两个结果:对于EntityRg,定义的range.address与AllEntRg相同(这是情况并非如此)。对于CostCRg,我遇到了错误。我不确定我没有正确定义。我已经在这里停留了很长一段时间,并且也尝试过使用Match Function。同样,单独运行也可以,但是在循环中,我得到了这些意外的结果。我对可能收到的反馈意见很感兴趣。谢谢。
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim wb As Workbook
Dim BudWkb As Workbook
Dim Wk2 As Worksheet
Dim PNLWkb As Workbook
Dim fpath As String
Dim fname As String
Set BudWkb = Workbooks("SubModel Forecast_Other Admin v4.xlsm")
Set Wk2 = BudWkb.Sheets("By PM")
fname = "Feb15 PNL"
'fname = InputBox("Enter PNL File Name")
Dim Wk4 As Worksheet
Set Wk4 = BudWkb.Sheets("Validation")
With Wk4
Dim CCCol As Long
Dim fRowCC As Long
Dim lRowCC As Long
CCCol = Wk4.Cells.Find("Cost Center", lookat:=xlWhole).Column
fRowCC = Wk4.Cells.Find("Cost Center", lookat:=xlWhole).Offset(1, 0).row
lRowCC = Wk4.Cells.Find("Cost Center", lookat:=xlWhole).End(xlDown).row
Dim CCRg As Range
Set CCRg = Wk4.Range(Wk4.Cells(fRowCC, CCCol), Wk4.Cells(lRowCC, CCCol))
'MsgBox (CCRg.Address)
End With
Set PNLWkb = Workbooks("Feb15 PNL.xlsx")
Dim Wk1 As Worksheet
Set Wk1 = PNLWkb.Sheets("det")
With Wk1
If Left(Wk2.Name, 5) = "By PM" Then
Dim OpsCol As Long
OpsCol = Wk1.Cells.Find("Property Manager", lookat:=xlWhole).Column
Else
OpsCol = Wk1.Cells.Find("Submarket", lookat:=xlWhole).Column
End If
Dim FRow As Long
Dim lRow As Long
Dim ExpCol As Long
Dim PropCodeCol As Long
Dim Expense As String
Expense = InputBox("Enter Expense GL")
'to locate begining and ending row of data on PNL report
'Identifies the column where the SubMarket names are located for lookup purposes
'Defines the expense GL column to lookup based on the inputbox above
FRow = Wk1.Cells.Find("66990000", lookat:=xlPart).Offset(2, 0).row
lRow = Wk1.Cells.Find("66990000", lookat:=xlPart).End(xlDown).Offset(-1, 0).row
ExpCol = Wk1.Cells.Find(Expense, lookat:=xlPart).Column
PropCodeCol = Wk1.Cells.Find("Property Code", lookat:=xlWhole).Column
'Defines the Range of the PM or Sub-Market Names
Dim OpsRg As Range
Set OpsRg = Wk1.Range(Wk1.Cells(FRow, OpsCol), Wk1.Cells(lRow, OpsCol))
'Defines the Range of the Property Codes
Dim PropCodeRg As Range
Set PropCodeRg = Wk1.Range(Wk1.Cells(FRow, PropCodeCol), Wk1.Cells(lRow, PropCodeCol))
'Defines the exact range of the expense column being analyzed
Dim ExpRg As Range
Set ExpRg = Wk1.Range(Wk1.Cells(FRow, ExpCol), Wk1.Cells(lRow, ExpCol))
End With
Dim AllEntRg As Range
For Each Cell In OpsRg
If Cell = "" Then
If AllEntRg Is Nothing Then
Set AllEntRg = Cells(Cell.row, PropCodeCol)
Else
Set AllEntRg = Union(AllEntRg, Cells(Cell.row, PropCodeCol))
End If
'End If
End If
Next
MsgBox (AllEntRg.Address)
'MsgBox (Application.Match(Wk1.Cells(59, 1), CCRg, 0))
'Dim y
'y = Application.Match(Wk1.Cells(10, 1), CCRg, 0)
'If IsError(y) Then
'MsgBox ("pooopy error")
'End If
Dim EntityRg As Range
'Dim c As Range
For Each c In AllEntRg
'Dim z
'z = Application.Match(c, CCRg, 0)
If CCRg.Find(c.Value, lookat:=xlPart) Is Nothing Then
If EntityRg Is Nothing Then
Set EntityRg = c
Else
Set EntityRg = Union(EntityRg, c)
End If
End If
Next
MsgBox (EntityRg.Address)
Dim CostCRg As Range
Dim r As Range
For Each r In AllEntRg
If Not CCRg.Find(r.Value, lookat:=xlPart) Is Nothing Then
If CostCRg Is Nothing Then
Set CostCRg = r
Else
Set CostCRg = Union(CostCRg, r)
End If
End If
Next
MsgBox (CostCRg.Address)
Dim v As Double
v = Application.WorksheetFunction.Sum(EntityRg)
'SendKeys "{F9}"
MsgBox (v)
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
我无法运行您的代码,但已对其进行了检查并注意到一些可能的问题。
lRowCC = Wk4.Cells.Find("Cost Center", lookat:=xlWhole).End(xlDown).row
`.End(xlDown)不是查找列最后一行的可靠方法。阅读我的答案以得到解释:Excel vba – xlDown
您说:“对于EntityRg,定义的range.address与AllEntRg相同(事实并非如此)。”
您是否相信它们是相同的,因为EntityRg.Address = AllEntRg.Address
?
EntityRg .Address
将是由逗号分隔的绝对单元格和范围地址的字符串。您可能不知道此字符串的最大长度约为255。我找不到任何文档,但是根据我自己的实验,该字符串EntityRg .Address
将被截断为小于256,从而没有部分单元格或范围地址。
您是否被这些地址匹配的前255个字符所迷惑?
另一种可能性是,每次使用的CCRg.Find(c.Value, lookat:=xlPart)
收益Nothing
,从而EntityRg
和AllEntRg
相等。你CostCRg
说错了;是因为是Nothing
吗?
您有两个循环在中搜索CCRg
值AllEntRg
。一个循环记录成功,一个循环记录失败。为什么不将循环组合成类似以下内容的内容:
If CCRg.Find(c.Value, lookat:=xlPart) Is Nothing Then
If EntityRg Is Nothing Then
Set EntityRg = c
Else
Set EntityRg = Union(EntityRg, c)
End If
Else
If CostCRg Is Nothing Then
Set CostCRg = r
Else
Set CostCRg = Union(CostCRg, r)
End If
End If
我担心For Each c In AllEntRg
没有给您您期望的东西。如果您将范围与结合使用Union
,则会整理整齐。因此Union(Range("A2"), Range("A3", Range("A5"), Range("A6"), Range("A7")).Address
,“ $ A $ 2:$ A $ 3,$ A $ 5:$ A $ 7”不是“ $ A $ 2,$ A $ 3,$ A $ 5,$ A $ 6,$ A $ 7”。我的回忆是For Each c In AllEntRg
不会将“ $ A $ 2:$ A $ 3”拆分为单独的单元格。
请F8用来逐步执行此循环,以检查它是否按预期执行。
希望这可以帮助
评论中描述的问题的答案
您的问题是您对With
s的使用不一致,尤其是,您没有标识要操作的工作簿。
Wk4
被明确指定为在工作簿内,BufdWkb
并且Wk1
被指定为在工作簿内PNLWkb
。
但是,在
Set AllEntRg = Cells(Cell.row, PropCodeCol)
您没有为指定工作表或工作簿Cells
。这相当于
Set AllEntRg = ActiveWorkbook.ActiveSheet.Cells(Cell.row, PropCodeCol)`
您需要编写Set AllEntRg = .Cells(Cell.row, PropCodeCol)
(注意单元格之前的句点),并将此代码包含在With Wk1
块中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句