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
相关产品推荐
相关产品推荐

