如果在其他工作簿中find或未find单元格,请创build各种范围

我一直在用我的代码挣扎了一天半。 我有一个包含超过50列18000行的电子表格。 我已经能够基于列H(OpsCol)中的空白单元格来确定由“AllEntRg”定义的A列中的较小范围的单元格。 我坚持我的循环底部。 对于EntityRg,我循环遍历“AllEntRg”中的每个单元格,如果它在BudWb Wk4中定义的范围CCRg中找不到,那么我想创build一个所有这些单元格的范围。 下一个选项CostCRg,我想为CCrg中的所有单元格定义一个范围。

我已经通过select单个单元格进行了testing,它提供了我正在寻找的结果,但是当我在循环中得到以下两个结果时:对于EntityRg,定义的range.address与AllEntRg相同(this不应该是这样)。 对于CostCRg,我得到一个错误。 我不确定我没有正确定义。 我在这里呆了一段时间,我也尝试过使用匹配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将是由逗号分隔的绝对单元格和范围地址的string。 您可能不知道该string的最大长度约为255.我找不到任何文档,但是从我自己的实验中, EntityRg .Address将被截断为小于256,因此没有部分单元格或范围地址。

你被这些地址的前255个字符匹配吗?

另一种可能性是每次使用CCRg.Find(c.Value, lookat:=xlPart)返回Nothing因此EntityRgAllEntRg是相等的。 你说CostCRg给出了一个错误; 这是因为它是Nothing


CCRg有两个循环searchCCRg的值。 一个循环logging成功,一个logging失败。 为什么不把这些循环合并成如下forms:

 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明确指定在工作簿BufdWkbWk1指定在PNLWkb内。

但是,在

 Set AllEntRg = Cells(Cell.row, PropCodeCol) 

您不指定Cells的工作表或工作簿。 这相当于

 Set AllEntRg = ActiveWorkbook.ActiveSheet.Cells(Cell.row, PropCodeCol)` 

您需要编写Set AllEntRg = .Cells(Cell.row, PropCodeCol) (单元格之前的注释周期),并将该代码包含在With Wk1 Block中。