excel - 如何在 VBA 中获取行范围 2 :60 and column range D:H 中单元格的最大值

标签 excel row range max vba

我有一个 Excel 表,其中包含 60 行和 7 列的整数值。第一行是评论员,前三列也是评论员(我必须省略那些)。

因此,我想遍历列范围 3:7 (D:H) 中的行范围 2:60 以找到一行中单元格的最大值。答案转到第一列中的单元格。

我所拥有的(简化 View ):

       Test1  Test2  Test3  Test4
One    1      2      3      4
Two    5      6      7      8
Three  1      2      5      4
Four   1      7      3      4

What I would like to have:

       Test1  Test2  Test3  Test4  Max
One    1      2      3      4      4
Two    5      6      7      8      8
Three  1      2      5      4      5 
Four   1      7      3      4      7

If I create a macro manually, I receive this:

range("D2:I2").Select
range("I2").Activate
ActiveCell.FormulaR1C1 = "=MAX(RC[-5]:RC[-1])"

range("D3:I3").Select
range("I3").Activate
ActiveCell.FormulaR1C1 = "=MAX(RC[-5]:RC[-1])"

range("D4:I4").Select
range("I4").Activate
ActiveCell.FormulaR1C1 = "=MAX(RC[-5]:RC[-1])"

酷......但我想用循环遍历它。

我试过这个,但不起作用:
Sub max()
    For num = 2 To 4
        range("D&num:I&num").Select
        range("I & num").Activate
        ActiveCell.FormulaR1C1 = "=MAX(RC[-5]:RC[-1])"
    Next num
End Sub

有任何想法吗 ?

最佳答案

Simoco 已经提到您的代码中的错误是什么。我还要补充一点,您应该避免使用 .Activate .您可能想查看 THIS

最简单的方法是使用不使用循环的 this

Sub Sample()
    With ThisWorkbook.Sheets("YourSheetName")
        .Range("I2:I60").Formula = "=Max(D2:H2)"
    End With
End Sub

关于excel - 如何在 VBA 中获取行范围 2 :60 and column range D:H 中单元格的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20968734/

相关文章:

PHP5 + PostgreSQL - pg_fetch_row() 返回字符串而不是数组

mysql - 如何为 MySQL 表添加索引?

scala - 如何高效/优雅地提取连续整数的范围?

google-sheets - 两种方式的 Google 电子表格无限范围

php - 将Excel数据插入MySQL

javascript - 如何将html表格中相同大小的图像导出到excel

Java Apache POI——创建一个迭代器来查找日期

java - 如何向 Tapestry Grid 添加额外的行?

javascript - 正则表达式验证范围内的日期年份

Excel: 'Unable to set the Calculation property of the Application class'