简体   繁体   English

Excel条件格式公式和范围(VBA)

[英]Excel Conditional formatting formula and ranges (VBA)

I have a FormatCondition . 我有一个FormatCondition I am reading the .Formula1 and the .AppliesTo 我正在阅读.Formula1.AppliesTo

I want to find if the Formula contains a cell reference that will change across the range specified by the AppliesTo, eg =NOT(ISNUMBER(C3)) in a range C3:D10 . 我想查找公式是否包含将在AppliesTo指定的范围内变化的单元格引用,例如CNO C3:D10范围内的=NOT(ISNUMBER(C3))

From what I can tell, that reference might be relative (eg C3 ) or mixed (eg $C3 or C$3 ) but not absolute. 据我所知,该引用可能是相对的(例如C3 )或混合的(例如$C3C$3 ),但不是绝对的。 It will also be the 'top-left most cell in the applied range' based on https://www.ablebits.com/office-addins-blog/2014/08/07/relative-absolute-cell-references-excel-conditional-formatting/ and some other sites. 根据https://www.ablebits.com/office-addins-blog/2014/08/07/relative-absolute-cell-references-excel-,它也将是“应用范围中最左上角的单元格” 条件格式/和其他一些网站。

If the range is something like C3:D10 then that's easy. 如果范围是C3:D10 ,那很容易。 Find the top left using eg myRange.Cells(1, 1) , convert it to a string using .Address or similar and then look for that string in the Formula, along with relevant variations adding a $ . 使用myRange.Cells(1, 1)查找左上角myRange.Cells(1, 1)使用.Address或类似名称将其转换为字符串,然后在公式中查找该字符串,以及添加$相关变体。 Sure, I'd have to be careful about things like strings (eg top left cell is C3' and formula is ="CC3NE"`), but it's possible (still, I'd welcome suggestions on how to do that better, but that's not the question). 当然,我必须注意类似字符串的事情(例如,左上方的单元格为C3' and formula is =“ CC3NE”`),但有可能(仍然,我欢迎提出有关如何更好地做到这一点的建议,但这不是问题)。

The real problem is if the range is more complex, as seems to happen often with Conditional Formatting. 真正的问题是范围是否更复杂(如条件格式设置经常发生的那样)。 An example is $CQ$39:$FT$39,$BE$39,$BE$8:$BE$9,$CU$8:$FT$9,$CU$12:$FT$14,$BE$12:$BE$14,$BE$16:$BE$30,$CQ$16:$FT$30,$CQ$32:$FT$36,$BE$32:$BE$36 . 例如$CQ$39:$FT$39,$BE$39,$BE$8:$BE$9,$CU$8:$FT$9,$CU$12:$FT$14,$BE$12:$BE$14,$BE$16:$BE$30,$CQ$16:$FT$30,$CQ$32:$FT$36,$BE$32:$BE$36 .Cells(1, 1) doesn't give the right answer for that ( BE8 , according to the formula that Excel made). .Cells(1, 1)不能给出正确的答案( BE8 ,根据Excel制定的公式)。

Further, what if the range were something like $A$5,$E$1 - there is no 'top left'. 此外,如果范围是$A$5,$E$1 -没有“左上方”怎么办? So how does excel (or the user) decide which should be in the formula? 那么,excel(或用户)如何确定公式中应该包含哪些内容?

Thus the primary question is: What is the system for Excel determining which cell to use in a formula, because I don't believe it is 'top left' - it might be 'find the topmost row, then find the leftmost cell there', or it might be the reverse, or something completely different. 因此,主要的问题是: Excel确定在公式中使用哪个单元格的系统什么,因为我不认为它是“左上角”的-它可能是“查找最上面的行,然后找到那里的最左边的单元格” ,或者相反,或者完全不同。

The second question is: how best can I find that cell from a given range? 第二个问题是: 如何最好地找到给定范围内的那个细胞?

The bonus question (I'm happy if this doesn't get answered here) is: Is there a good way to then find references to that cell in a formula, including relative and mixed versions of the cell? 额外的问题(如果在这里没有得到回答,我很高兴)是: 有一种好的方法可以在公式中查找对该单元格的引用,包括该单元格的相对和混合版本吗?

Note that these strange ranges are coming about from Rows and Columns being Cut and Pasted on a sheet with Conditional Formatting, with Excel chopping up the conditional formatting as a result (changing the range, and changing the formula, both without user input). 请注意,这些奇怪的范围来自使用条件格式在工作表上剪切和粘贴的行和列,而Excel则将条件格式切碎(更改范围和更改公式,这两种操作都无需用户输入)。 So the priority is to deal with whatever Excel would set the formula to be on its own in such a scenario, not necessarily what a user might do - but handling both would be even better. 因此,在这种情况下,优先考虑的是处理将Excel自行设置公式的任何事情,不一定是用户可能会做的事情-但同时处理两者会更好。

First answer : Short version: Excel does indeed use the most top left cell - but it does that EVEN IF THE CELL IS NOT PART OF THE RANGE . 第一个答案 :简短版本:Excel确实的确使用了最左上角的单元格-但是即使该单元格不在该范围内,它也会这样做

I created a blank sheet, selected A5,E1 , and put in a conditional format of NOT BLANK (which Excel does with a formula). 我创建了一张空白纸,选择了A5,E1 ,并以条件格式为NOT BLANK(Excel使用公式对其进行了处理)。 Excel created the following: Excel创建了以下内容:

Range: $E$1,$A$5 范围: $E$1,$A$5

Formula: =LEN(TRIM(A1))>0 公式: =LEN(TRIM(A1))>0

So even though A1 is not part of the range, that's what got used in the formula. 因此,即使A1不在范围内,这也是公式中使用的内容。

I checked this by duplicating it around the sheet, and it stayed consistent. 我通过在工作表周围复制来进行检查,并保持一致。

Thus Excel finds the leftmost column in the range, and the topmost row in the range, and combines them to produce the cell that should be considered the 'top left' of the range, even if the resulting cell is not actually part of the range. 因此,Excel在范围内找到最左边的列,在范围内找到最上面的行,并将它们合并以生成应被视为范围“左上角”的单元格,即使所得单元格实际上不是范围的一部分。

This suggests an answer to the second question too: Find the top most row in the range; 这也暗示了第二个问题的答案:找到范围中最上面的行; find the left most column; 找到最左边的列; then use a cell that combines the two to compare with the formula. 然后使用将两者结合起来的单元格与公式进行比较。 You could do that by going through the range string, or maybe cell by cell through the range itself; 您可以通过遍历范围字符串来做到这一点,或者遍历范围本身来逐个单元; perhaps there is a better way to do it (suggestions welcome). 也许有更好的方法可以做到(欢迎提出建议)。

It's not pretty, but it would work, if I can find a good way to do those two things. 它虽然不漂亮,但如果我能找到一种很好的方法来完成这两件事,那它就会起作用。

I would still welcome: 我仍然欢迎您:

  • anyone who can improve on this answer about which cell is used in the formula (I did not test this extensively) 任何人都可以改进此公式中有关在公式中使用哪个单元格的答案(我没有对此进行广泛的测试)
  • anyone who can suggest a good way to find the 'top left cell' as above 任何可以建议找到上述“左上方单元格”的好方法的人
  • anyone who can suggest a good way to search the formula string for that top left cell 任何可以建议一种在公式字符串中搜索左上方单元格的好方法的人

EDIT - Here's an alternate code method to Usmanhaq's excellent method - this goes through the range as a string: 编辑-这是Usmanhaq的出色方法的替代代码方法-这作为字符串遍历整个范围:

Private Function FindTopLeft(rangeStr As String, rowabs As Boolean, colabs As Boolean) As String
    newRange = Replace(rangeStr, ":", ",")
    newRangeArray = Split(newRange, ",")
    Dim lowestRow As Long
    Dim lowestCol As Long
    lowestRow = 2147483647
    lowestCol = 2147483647

    For Each cell In newRangeArray
        cell = Trim(cell)
        If cell <> "" Then
            cCol = range(relativeCell).Column
            cRow = range(relativeCell).Row
            If cCol < lowestCol Then lowestCol = cCol
            If cRow < lowestRow Then lowestRow = cRow
        End If
    Next

    FindTopLeft = Cells(lowestRow, lowestCol).Address(rowabs, colabs)

End Function

Thanks for updating your question and find below my comments 感谢您更新您的问题并在我的评论下方找到

What is the system for Excel determining which cell to use in a formula, because I don't believe it is 'top left' - it might be 'find the topmost row, then find the leftmost cell there', or it might be the reverse, or something completely different. Excel确定在公式中使用哪个单元格的系统是什么,因为我不认为它是“左上角”的-它可能是“查找最上面的行,然后找到最左边的单元格”,或者它可能是反向,或完全不同的东西。

Usually range in Excel is similar to a box or rectangle with a fixed length and width, eg B3:C8. 通常,Excel中的范围类似于具有固定长度和宽度的框或矩形,例如B3:C8。 In case of this simple box type ranges the top left most cell is the base for all formulas in conditional formatting. 在这种简单的框类型范围的情况下,最左上角的单元格是条件格式中所有公式的基础。 In other words at first formula evaluation this cell's value is checked against the formula applied in conditional formating. 换句话说,在第一次公式评估时,将根据条件格式中应用的公式检查此单元格的值。 (Let's call this cell as mother cell) (我们称此单元为母单元)

For example 例如

  • If conditional formatting is applied to cell A1:D10 and a conditional formula is B2 > 0, it means apply formatting to cell A1 if value of B2 > 0, and for others apply formatting to any cell if value of a cell with an offset of (1, 1) (that was B2 in A1 case) has value greater then 0. ie for formatting of B2 value of C3 will be checked. 如果将条件格式应用于单元格A1:D10并且条件公式为B2> 0,则意味着如果B2> 0,则将格式应用于单元格A1;对于其他单元格,如果偏移量为的单元格将格式应用于任何单元格(1,1)(在A1情况下为B2)的值大于0。即,对于B2的格式,将检查C3的值。
  • If conditional formatting is applied to cell A1:D10 and a conditional formula is AND($B1 >= 30, $B1 <= 60) it means apply formatting to cell A1 if value of condition is true, but now the difference comes, because B is now static because of $B, for conditional formatting of B2 the condition will again be AND($B1 >= 30, $B1 <= 60) same for C1 and D1, but for A2, B2, C2 and D2 it will be AND($B2 >= 30, $B2 <= 60) 如果将条件格式应用于单元格A1:D10并且条件公式为AND($ B1> = 30,$ B1 <= 60),则意味着如果condition的值为true,则将格式应用于单元格A1,但是现在有所不同,因为由于$ B,B现在是静态的,对于B2的条件格式,条件将再次为AND($ B1> = 30,$ B1 <= 60)对于C1和D1相同,但是对于A2,B2,C2和D2是AND($ B2> = 30,$ B2 <= 60)

In first case the first cell that was checked against the formula was A1 and even in second case the cell that was checked against the conditional formula was A1 (which we called mother cell) 在第一种情况下,第一个根据公式检查的单元格为A1,甚至在第二种情况下,根据条件公式检查的单元格为A1(我们称为母单元格)

Now if the range is not like a box, it is a mixed range separated by commas, even in this case if we make a box around that range the mother cell is the cell in upper left corner, it is not the first cell of the range but first cell of the box. 现在,如果该范围不像一个框,则它是一个用逗号分隔的混合范围,即使在这种情况下,即使我们围绕该范围创建一个框,其母单元格也是左上角的单元格,也不是该单元格的第一个单元格。范围,但框的第一个单元格。

For your 2nd question how best can I find that cell from a given range? 对于第二个问题,我如何才能最好地找到给定范围内的那个单元格?

If you want to find the mother cell in a box like simple Range, it is just cells (1, 1) 如果您想在像简单范围之类的框中找到母单元,那就是单元(1,1)

If you want to find that cell in mixed range may be there are some functions out there but I don't know of any; 如果要查找混合范围内的单元格,可能有一些功能,但我不知道。 I would find that by using loop like below 我会通过使用下面的循环来找到

Sub find_topleft()

 Dim r, full_range As Range

 Set r = Range("$O$8:$O$17,$Q$4:$Q$13,$S$4:$S$6")

 Set full_range = Sheets("Sheet1").UsedRange

 row_num = full_range.Rows.Count + full_range.Row - 1
 col_num = full_range.Columns.Count + full_range.Column - 1

 For Each c In r
    If c.Row < row_num Then
    row_num = c.Row
    End If

    If c.Column < col_num Then
    col_num = c.Column
    End If
 Next

 Debug.Print row_num
 Debug.Print col_num

End Sub

Is there a good way to then find references to that cell in a formula, including relative and mixed versions of the cell? 有什么好方法可以在公式中查找对该单元格的引用,包括该单元格的相对版本和混合版本吗?

That cell is directly linked with the formula as stated in answer to your first question. 如您对第一个问题的回答所述,该单元格直接与公式链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM