【问题标题】:Excel Macro: is it possible to find Row heights and Column widths of a worksheet?Excel 宏:是否可以找到工作表的行高和列宽?
【发布时间】:2011-04-16 06:57:48
【问题描述】:

有没有什么方法可以在 Excel 工作表中找到从 1 到 50 的 ROWS 的高度,以及从 A 到 Z 的 COLUMNS 的宽度,而无需手动单击并写下答案?

谢谢

迈克尔。

【问题讨论】:

    标签: excel automation


    【解决方案1】:

    在@Parkyprg 之上添加

    Range("A1:A50").Height      ' height of range
    Range("A:Z").Width          ' width of range
    Range("A1:A50").Rows.Count  ' number or rows in range
    Range("A3").Height          ' height of A3 cell
    Range("A3").Width           ' width of A3 cell
    

    【讨论】:

      【解决方案2】:

      在 Excel VBA 中,您可以使用如下内容:

      Range("A1:A50").Height  
      

      和

      Range("A:Z").Width
      

      【讨论】:

      • 那只是给出总高度/宽度吗?是否有可能找出该范围内的行数?或者可能是每行/列的高度/宽度?​​
      • 对于一行使用 Range("A1").Height (或任何其他数字),对于宽度: Range("A").Width
      【解决方案3】:

      在 Excel vba 中,使用 rows.height 属性很容易迭代获取行高的输出:

      Sub outputRowHeight()
          Dim i As Integer
      
          For i = 1 To 50
               Debug.Print Rows(i).Address & " is : " & Rows(i).Height
          Next i
      
      End Sub
      

      对于列宽,我们使用 columns.width 属性。这有点棘手,因为您不能直接遍历字母表中的字母,所以我们需要使用索引来代替:

      Sub outputColumnWidths1()
          Dim i As Integer
      
          For i = 1 To 26 '26th letter is Z
                  Debug.Print Columns(i).Address & " is : " & Columns(i).Width
          Next i
      
      End Sub
      

      如果您想避免必须知道字母的索引,可以使用 if 语句在超出范围时退出:

      Sub outputColumnWidths2()
          Dim i As Integer
      
          For i = 1 To 99 'using 99 as arbitrary high end number
              If Columns(i).Address <> "$AA:$AA" Then
                  Debug.Print Columns(i).Address & " is : " & Columns(i).Width
              Else 'once we get outside of A-Z, we want to end.
                  Exit For
              End If
          Next i
      
      End Sub
      

      这将产生与第一个示例相同的结果,但您可以通过增加迭代次数和您不想评估的第一个列的第一列名称来将其扩展到您想要的列。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-21
        • 2021-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-10
        • 1970-01-01
        相关资源
        最近更新 更多