运行-WorksheetFunction 上的时间错误“1004”。Max/Min 变量范围
Run-time error '1004' on WorksheetFunction.Max/Min for variable range
我想在 for 循环中获取范围的最大值和最小值(对于每一行)。
我在互联网上找到的解决方案要么是静态范围的,要么给我同样的错误。目前我使用以下在 xMax
行给出 run-time error '1004'
:
Dim i As Long
Dim xMax As Double
Dim ws1 As Worksheet
Set ws1 = Worksheets("Sheet1")
For i = 2 To 15
xMax = Application.WorksheetFunction.Max(ws1.range(Cells(i, 6), Cells(i, 15)))
Next
但是以下静态范围有效:
xMax = Application.WorksheetFunction.Max(ws1.range(Cells(2, 6), Cells(2, 15)))
我试图将它简化为 2 个单元格:
静态范围没有问题:
xMax = Application.WorksheetFunction.Max(Cells(2, 6).Value, Cells(2, 7).Value)
但是动态范围弹出同样的错误:
For i = 2 To 15
xMax = Application.WorksheetFunction.Max(Cells(i, 6).Value, Cells(i, 7).Value)
Next
我怎样才能在一个循环中实现 min/max 的变量范围?
我包含了一个拆分功能,可以更轻松地捕获字母。
Dim i As Long
Dim xMax As Double
Dim ws1 As Worksheet
Set ws1 = Worksheets("Sheet1")
For i = 2 To 15
xMax = Application.WorksheetFunction.Max(ws1.range("F" & i & ":" & Split(Cells(, i).Address, "$")(1) & "15"))
Next
你必须告诉 Excel cell
指向哪里 - 否则它将使用活动的 sheet,例如
xMax = Application.WorksheetFunction.Max(ws1.range(ws1.Cells(i, 6), ws1.Cells(i, 15)))
或
With ws1
xMax = Application.WorksheetFunction.Max(.Range(.Cells(i, 6), .Cells(i, 15)))
End With
就个人而言,我通常首先将范围分配给范围变量 - 它有助于调试:
With ws1
dim r as range
''' set r = .Range(.Cells(i, 6), .Cells(i, 15)) ' Fails if not all cells are numeric
set r = .Range(.Cells(i, 6), .Cells(i, 15)).SpecialCells(xlCellTypeConstants, xlNumbers)
xMax = Application.WorksheetFunction.Max(r)
End With
在确定所有范围后开始您的初始陈述:
xMax = Application.WorksheetFunction.Max(ws1.range(ws1.Cells(i, 6), ws1.Cells(i, 15)))
当范围内的某些单元格有错误时,例如 #NA
或 #DIV!0
,Max
函数会引发错误。要让您的计算忽略范围内的错误单元格,您可以使用 Aggregate
函数。
xMax = Application.Aggregate(14, 6, ws1.range(ws1.Cells(i, 6), ws1.Cells(i, 15)), 1)
- 第一个参数
14
指定Large
。 Small
. 使用 15
- 第二个参数
6
指定"ignore errors in the computation"
- 最后一个参数
1
指定"First largest result"
另请注意您尝试的其他版本,即
xMax = Application.WorksheetFunction.Max(Cells(i, 6).Value, Cells(i, 7).Value)
不指定连续范围,而是指定一组值(变体)。只要这些变体之一不是数字,就会引发错误。但是你不需要这个版本。
我想在 for 循环中获取范围的最大值和最小值(对于每一行)。
我在互联网上找到的解决方案要么是静态范围的,要么给我同样的错误。目前我使用以下在 xMax
行给出 run-time error '1004'
:
Dim i As Long
Dim xMax As Double
Dim ws1 As Worksheet
Set ws1 = Worksheets("Sheet1")
For i = 2 To 15
xMax = Application.WorksheetFunction.Max(ws1.range(Cells(i, 6), Cells(i, 15)))
Next
但是以下静态范围有效:
xMax = Application.WorksheetFunction.Max(ws1.range(Cells(2, 6), Cells(2, 15)))
我试图将它简化为 2 个单元格:
静态范围没有问题:
xMax = Application.WorksheetFunction.Max(Cells(2, 6).Value, Cells(2, 7).Value)
但是动态范围弹出同样的错误:
For i = 2 To 15
xMax = Application.WorksheetFunction.Max(Cells(i, 6).Value, Cells(i, 7).Value)
Next
我怎样才能在一个循环中实现 min/max 的变量范围?
我包含了一个拆分功能,可以更轻松地捕获字母。
Dim i As Long
Dim xMax As Double
Dim ws1 As Worksheet
Set ws1 = Worksheets("Sheet1")
For i = 2 To 15
xMax = Application.WorksheetFunction.Max(ws1.range("F" & i & ":" & Split(Cells(, i).Address, "$")(1) & "15"))
Next
你必须告诉 Excel cell
指向哪里 - 否则它将使用活动的 sheet,例如
xMax = Application.WorksheetFunction.Max(ws1.range(ws1.Cells(i, 6), ws1.Cells(i, 15)))
或
With ws1
xMax = Application.WorksheetFunction.Max(.Range(.Cells(i, 6), .Cells(i, 15)))
End With
就个人而言,我通常首先将范围分配给范围变量 - 它有助于调试:
With ws1
dim r as range
''' set r = .Range(.Cells(i, 6), .Cells(i, 15)) ' Fails if not all cells are numeric
set r = .Range(.Cells(i, 6), .Cells(i, 15)).SpecialCells(xlCellTypeConstants, xlNumbers)
xMax = Application.WorksheetFunction.Max(r)
End With
在确定所有范围后开始您的初始陈述:
xMax = Application.WorksheetFunction.Max(ws1.range(ws1.Cells(i, 6), ws1.Cells(i, 15)))
当范围内的某些单元格有错误时,例如 #NA
或 #DIV!0
,Max
函数会引发错误。要让您的计算忽略范围内的错误单元格,您可以使用 Aggregate
函数。
xMax = Application.Aggregate(14, 6, ws1.range(ws1.Cells(i, 6), ws1.Cells(i, 15)), 1)
- 第一个参数
14
指定Large
。Small
. 使用 - 第二个参数
6
指定"ignore errors in the computation" - 最后一个参数
1
指定"First largest result"
15
另请注意您尝试的其他版本,即
xMax = Application.WorksheetFunction.Max(Cells(i, 6).Value, Cells(i, 7).Value)
不指定连续范围,而是指定一组值(变体)。只要这些变体之一不是数字,就会引发错误。但是你不需要这个版本。