打印本文 打印本文 关闭窗口 关闭窗口
vba返回Excel多个不连续选择区域的总行数
作者:武汉SEO闵涛  文章来源:敏韬学习网  点击数837  更新时间:2010/6/21 21:22:01  文章录入:mintao  责任编辑:mintao

  如何通过VBA,返回Excel中已经选择的多个不连续的选择区域的行的数量?

  代码如下,希望对您有所帮助。

  ①假定选择区域

  A:假定您已通过鼠标在Excel中选择好了多个不连续的选择区域;

  B:假定您已使用如下代码在Excel中选择好了多个不连续的选择区域:

  Dim MyArea1, MyArea2, MyArea3 As Range
  Set MyArea1 = Range(Cells(1, 1), Cells(3, 3))
  Set MyArea2 = Range(Cells(6, 1), Cells(9, 3))
  Set MyArea3 = Range(Cells(12, 1), Cells(13, 3))
  Application.Union(MyArea1, MyArea2, MyArea3).Select

  ②通过如下代码返回多个非连续区域的行的总数

  Dim I, MyRowTotal

  For I = 1 To Selection.Areas.Count

   MyRowTotal = MyRowTotal + Selection.Areas(I).Rows.Count

   Next

  MsgBox MyRowTotal

  通过如上代码,将多个不连续选择区域的总行数,保存在MyRowTotal变量中,这样,我们就可以在代码中的任何一个地方,调用此MyRowTotal的数据。

打印本文 打印本文 关闭窗口 关闭窗口