如何在.FIND中使用MAX?在VBA(Excel)中

时间:2018-09-20 21:52:34

标签: vba excel-vba

我已经在这个问题上工作了一段时间,但似乎无法使其正常工作。我有以下VBA代码,我想获取range1的Resize的最大值。我尝试过将MAX放在开始,结束以及其他我能想到的其他地方。

这是现在没有MAX的情况:

On Error Resume Next
    For Each ws In ActiveWorkbook.Worksheets
         If ws.Name <> "BHAInfo"  And ws.Name <> "BHADetail" Then
           Set rng1 = ws.Cells.Find(What:="Total Length").Resize(15)
          Set rng2 = Sheets("BHAInfo").Cells(Rows.Count, "J").End(xlUp).Offset(1)
          Rng.Value = rng1.Value
        End If
    Next

这是我最近一次修改上面的代码以包含MAX的尝试:

On Error Resume Next
        For Each ws In ActiveWorkbook.Worksheets
             If ws.Name <> "BHAInfo" And ws.Name <> "BHADetail" Then
               Set rng1 = ws.Cells.Find.FormuaArray = "=MAX(What:= ""Total Length"")).Resize(15)"
              Set rng2 = Sheets("BHAInfo").Cells(Rows.Count, "J").End(xlUp).Offset(1)
              rng2.Value = rng1.Value
            End If
        Next

1 个答案:

答案 0 :(得分:2)

类似这样的东西:

    For Each ws In ActiveWorkbook.Worksheets
         If ws.Name <> "BHAInfo" And ws.Name <> "BHADetail" Then

           Set rng1 = ws.Cells.Find(What:="Total Length")
           If Not rng1 Is Nothing Then
               Sheets("BHAInfo").Cells(Rows.Count, "J").End(xlUp).Offset(1).Value = _
                    Application.Max(rng1.Resize(15,1))
           End If

        End If
    Next