Vba 命名范围中的最小值不断返回不正确的值
我有一个包含超过一百万行数据的电子表格,正在尝试确定临时命名范围内的最小值。但是,当范围中的所有值都大于1时,它将保持返回0值。作为第二项检查,我在excel电子表格中使用了Min公式,并检查最小值是否大于1 下面是我的Excel VBA代码 使用Vba 命名范围中的最小值不断返回不正确的值,vba,excel,Vba,Excel,我有一个包含超过一百万行数据的电子表格,正在尝试确定临时命名范围内的最小值。但是,当范围中的所有值都大于1时,它将保持返回0值。作为第二项检查,我在excel电子表格中使用了Min公式,并检查最小值是否大于1 下面是我的Excel VBA代码 使用 ActiveCell.Value=Application.WorksheetFunction.MinRangemyRange下面的代码将获得U列中该列中所有正值的最小值 代码 使用我的U列中的数据样本运行此代码时的结果 检查代码之后;这有很多问题。。
ActiveCell.Value=Application.WorksheetFunction.MinRangemyRange下面的代码将获得U列中该列中所有正值的最小值 代码 使用我的U列中的数据样本运行此代码时的结果
检查代码之后;这有很多问题。。。 如果U列范围内的每个单元格大于0,则为1;您可以使用ActiveCell.Offset0,-4.RangeA1.Select不需要.RangeA1或.Select 2\将变量Dim BlankCells设置为Integer BlankCells=ActiveCell.Value,但不使用它。 3\然后再次偏移,ActiveCell.Offset0,-12.RangeA1.Select 4_u1和3实际上抵消了-16列,这将把你放在E列而不是B列,就像你在评论中说的那样 5_u然后尝试从E列中的活动单元格设置临时范围;但是您从未在temprownrst=ActiveCell.Row-Temp中定义过变量Temp,如果Temp变量大于8,则代码将失败,因为您在单元格U8上启动的范围小于0行。 6_uu中断尝试定义临时范围MyRange range CellsTempRownrStart、TempColNr、CellsTempRowNrLast、TempColNr.Name=MyRange的代码行 7_uu然后尝试将最小值写入正在检查的行上E列的R列13列 如果您真的试图完成我从这种混乱中理解的内容,请查看我的代码。它只是一个帮助你的指南,如果它对你没有帮助,更新你的问题,以更清楚地了解你想要完成什么
可能字段的格式为文本。您应该避免使用Select、ActiveCell,而应使用完全限定的单元格和范围。另外,这个ActiveCell.Offset0,-4.RangeA1.Select假设是什么?你想用这条线干什么?为什么不?我在格式化代码,你在写答案,而我在写完整答案@ShaiRado,我被一些“重复”警报阻止了!
Sub MinValuesTemRange()
' Macro determines the minimum value in a temporary range
'Step 1: Declare variables
Dim CloseOutRange As Range
Dim CloseOutCell As Range
'Step 2: Define the Target Range
Set CloseOutRange = Range("U8:U10201")
'Step 3: Start looping through the range
For Each CloseOutCell In CloseOutRange
CloseOutCell.Select
If CloseOutCell.Value > 0 Then
'Step 4: Define the temporary variables for row and column numbers for the temporary Range
Dim TempRowNrStart As Integer
Dim TempColNr As Integer
Dim TempRowNrLast As Integer
'Step 5: Obtain the already calculated number of blank cells between closeOutCells having values
ActiveCell.Offset(0, -4).Range("A1").Select
ActiveCell.Select
Dim BlankCells As Integer
BlankCells = ActiveCell.Value
'Step 6: Obtain the row number and column number of the active cell in Column B, the last row of the required range
ActiveCell.Select
ActiveCell.Offset(0, -12).Range("A1").Select
TempRowNrLast = ActiveCell.Row
TempColNr = ActiveCell.Column
TempRowNrStart = ActiveCell.Row - Temp
'Step 7: Name the temporary range
Range(Cells(TempRowNrStart, TempColNr), Cells(TempRowNrLast, TempColNr)).Name = "MyRange"
'Step 8: Insert the minimum value in the desired cell
ActiveCell.Select
ActiveCell.Offset(0, 13).Range("A1").Select
ActiveCell.Value = Application.WorksheetFunction.Min(myRange)
End If
'Step 7: Get the next cell in the range
Next CloseOutCell
End Sub
Option Explicit
Sub MinValuesTemRange()
' Macro determines the minimum value in a temporary range
' Step 1: Declare variables
Dim CloseOutRange As Range
Dim CloseOutCell As Range
Dim PositiveRng As Range
' Step 2: Define the Target Range
Set CloseOutRange = Range("U8:U10201")
' Step 3: Start looping through the range
For Each CloseOutCell In CloseOutRange
If CloseOutCell.Value > 0 Then
If Not PositiveRng Is Nothing Then
Set PositiveRng = Application.Union(PositiveRng, CloseOutCell)
Else
Set PositiveRng = CloseOutCell
End If
End If
Next CloseOutCell
' check if there is at least 1 cell in column U with positive value
If Not PositiveRng Is Nothing Then
MsgBox "Min value is :" & WorksheetFunction.Min(PositiveRng)
Else
MsgBox "All cells in column 'U' are empty or less than 0", vbInformation
End If
End Sub
Sub test()
Dim myRange As Range
Dim MinValCel As Range
Dim MinVal As Long
For Each cell In Range("U8:U10201")
If cell.Value > 0 Then
cell.Offset(, -16).Select
'set up cell to hold the minimum value
Set MinValCel = ActiveCell.Offset(, 13)
'set up MyRange
Set myRange = Range(ActiveCell, ActiveCell.Offset(-2, 0)) 'Range(ActiveCell, ActiveCell.Offset(-2, 0))
'MsgBox myRange.Address (used to test the range)
'set up Min worksheetfunction
MinVal = Application.WorksheetFunction.Min(myRange)
'write the minimum value from range
MinValCel.Value = MinVal
End If
Next
End Sub