You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel公式求助:跨工作表获取Sheet2中Power列的最大值

VBA 解决方案代码
Sub GetPowerMaxValue()
    Dim ws As Worksheet
    Dim powerCol As Integer
    Dim maxValue As Double
    
    ' 指定目标工作表
    Set ws = ThisWorkbook.Worksheets("Sheet2")
    
    ' 查找表头为"Power"的列号
    On Error Resume Next
    powerCol = ws.Rows(1).Find(What:="Power", LookIn:=xlValues, LookAt:=xlWhole).Column
    On Error GoTo 0
    
    ' 判断是否找到目标列
    If powerCol = 0 Then
        MsgBox "未找到表头为'Power'的列", vbExclamation
        Exit Sub
    End If
    
    ' 获取该列的最大值
    maxValue = Application.WorksheetFunction.Max(ws.Columns(powerCol))
    
    ' 输出结果(可根据需求改成写入单元格或其他操作)
    MsgBox "Power列的最大值为: " & maxValue
End Sub

代码说明

  • Set ws = ThisWorkbook.Worksheets("Sheet2"):锁定要操作的工作表,避免当前工作表切换导致错误
  • Rows(1).Find(...):在第1行精准匹配表头"Power",返回对应列号;加错误处理防止找不到时程序崩溃
  • Application.WorksheetFunction.Max(ws.Columns(powerCol)):调用Excel内置的MAX函数,直接计算目标列的最大值
  • 最后用MsgBox展示结果,你也可以改成ws.Range("A1").Value = maxValue这类写入单元格的操作

内容的提问来源于stack exchange,提问作者jhoybs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 10:18:11