我有一个超过一百万行数据的电子表格,我正在尝试确定临时命名范围内的最小值。但是,当范围中的所有值都超过0
时,它会保持返回1
值。作为第二项检查,我在Excel电子表格中使用了Min
公式,并检查了最小值实际上高于1
以下是我的 Excel VBA代码
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
答案 0 :(得分:1)
使用
ActiveCell.Value = Application.WorksheetFunction.Min(Range(“myRange”))
答案 1 :(得分:0)
对于该列中的所有正值,下面的代码将为“U”列提供最小值。
<强> 代码 强>
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
使用我的专栏U
中的数据样本运行此代码时的结果答案 2 :(得分:0)
审核您的代码后;这有很多问题......
1_对于列“U”范围内的每个单元格,如果它> 0;你使用ActiveCell.Offset(0, -4).Range("A1").Select
(不需要.Range(“A1”或.Select)
2_您设置变量Dim BlankCells As Integer BlankCells = ActiveCell.Value
但不使用它。
3_然后再次偏移,ActiveCell.Offset(0, -12).Range("A1").Select
4_使用1和3实际上是偏移-16列,这将使您进入“E”列而非“B”列,就像您在评论中所说的那样
5_然后,您尝试在“E”列中设置活动单元格的临时范围;但是你从未在TempRowNrStart = ActiveCell.Row - Temp
中定义变量“Temp”,如果“Temp”变量大于8,则代码将失败,因为你在单元格U8上开始你的范围使它小于0行。
6_打破您尝试定义临时范围的代码行“MyRange”Range(Cells(TempRowNrStart, TempColNr), Cells(TempRowNrLast, TempColNr)).Name = "MyRange"
7_然后,您尝试将最小值写入要检查的行中的“R”列(“E”列中的13列)。
如果你真的试图通过这种混乱来完成我的理解,请查看我的代码。它只是一个帮助您的指南,如果它对您没有帮助,请更新您的问题,以便更清楚地了解您要完成的任务。
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