简体   繁体   English

合并单元格数量的Excel公式?

[英]An Excel formula for number of merged cells?

Is there a formula in Microsoft Excel that takes a reference to a cell as argument and returns the number of cells merged into one? Microsoft Excel 中是否有公式将单元格的引用作为参数并返回合并为一个的单元格数? For example, if I merged 3 cells, the formula returns 3 for that cell;例如,如果我合并了 3 个单元格,则该公式为该单元格返回 3; if I merged 2 × 4 cells area, the formula returns 8, and so on…如果我合并 2 × 4 单元格区域,则公式返回 8,依此类推……

Consider this tiny UDF (User Defined Function):考虑这个微小的UDF (用户定义函数):

Public Function MergeSize(r As Range) As Long
    MergeSize = r(1).MergeArea.Cells.Count
End Function

If the UDF returns 1, the cell stands alone!如果 UDF 返回1,则该单元格独立!

User Defined Functions (UDFs) are very easy to install and use:用户定义函数 (UDF) 非常易于安装和使用:

  1. ALT-F11 brings up the VBE window ALT-F11 调出 VBE 窗口
  2. ALT-I ALT-M opens a fresh module ALT-I ALT-M 打开一个新模块
  3. paste the stuff in and close the VBE window粘贴内容并关闭 VBE 窗口

If you save the workbook, the UDF will be saved with it.如果您保存工作簿,UDF 将与它一起保存。 If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx如果您使用的是 2003 年以后的 Excel 版本,则必须将文件另存为.xlsm而不是.xlsx

To remove the UDF:要删除 UDF:

  1. bring up the VBE window as above调出如上的 VBE 窗口
  2. clear the code out清除代码
  3. close the VBE window关闭 VBE 窗口

To use the UDF from Excel:要使用 Excel 中的 UDF:

=MergeSize(A1) =合并大小(A1)

To learn more about macros in general, see:要了解有关一般宏的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/getstarted.htm http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs:以及关于 UDF 的细节:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!必须启用宏才能使其工作!

Excel does not have such a function built-in but it would be easy enough to write one. Excel 没有内置这样的函数,但编写一个很容易。

Function mergedcells(rng As Range) As Integer
    mergedcells = rng.MergeArea.Cells.Count
End Function

Using the offset function can help in a situation if you have data in the column to the left (or below) the this formula counts the number of populated cells a set distance away from the merged cells.如果左侧(或下方)列中有数据,则使用偏移函数会有所帮助,此公式计算距合并单元格一定距离的填充单元格数。

Try the following =COUNTA(OFFSET('[merged cell range],0,-1))试试下面的 =COUNTA(OFFSET('[merged cell range],0,-1))

the -1 is moving the count to one column to the left if there was data was below the ending would be ,1,0)如果数据低于结尾,则 -1 将计数向左移动一列,则为 ,1,0)

if there are only blanks cells around then try the formula如果周围只有空白单元格,请尝试公式

=COUNTBLANK(OFFSET('[merged cell range],0,-1)) =COUNTBLANK(OFFSET('[合并单元格范围],0,-1))

if there is mix of empty and populated cells add the formulas together如果混合了空单元格和填充单元格,则将公式添加在一起

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

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