【问题标题】:how to get smallest (min) & largest (max) value in a column in specific sheet using by VBA如何使用 VBA 在特定工作表的列中获取最小(最小值)和最大(最大值)值
【发布时间】:2016-10-02 03:39:19
【问题描述】:

我的代码有问题,它无法在 A 列中显示最小值和最大值。我不知道缺少或错误的代码在哪里...

我的程序打开 txt 文件并将数据输入到 Excel 表中。例如A列的数据是:

0.23
0.19
0.19
0.13
0.15
0.18
0.19
0.25
0.25
0.22
0.13

然后我在 VBA 中输入我的代码:

Private Sub CommandButton1_Click()

Dim vMin, vMax
Dim mg As Range
Dim NOR, lastrow, currentrow As Long

filetoopen = Application.GetOpenFilename("Text File (*.txt),*.txt", , "Select", , False)

If VarType(filetoopen) = vbBoolean Then
    Exit Sub
End If

Workbooks.OpenText filetoopen, Origin _
    :=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
    , ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:= _
    False, Space:=False, other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1) _
    , Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
    Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1)), _
    TrailingMinusNumbers:=True

'get number of rows (row with value inside)-------------
With ActiveSheet
   NOR = .Cells(Rows.Count, "A").End(xlUp).Row
End With

'GET SMALLEST & LARGEST VALUE FROM COLUMN A==========
With ActiveSheet
    lastrow = NOR

    For currentrow = 2 To lastrow
        Set mg = ThisWorkbook.Sheets(1).Rows(currentrow)

        'if row no data then no read------------------------
        If WorksheetFunction.CountA(mg) = 0 Then

        Else
            vMin = Application.WorksheetFunction.Min(Columns("A"))
            vMax = Application.WorksheetFunction.Max(Columns("A"))
        End If
    Next currentrow

End With

MsgBox "Minimum =  " & vMin & ", " & "Maximum =  " & vMax, vbInformation
MsgBox "last row A is = " & NOR

End Sub

如果我运行此代码,MessageBox 无法在 A 列中显示最小(最小)值和最大(最大)值。

希望你能帮我解决问题。

aaf

【问题讨论】:

    标签: vba excel excel-2003


    【解决方案1】:

    我已经尝试过你的代码......并且有两个想法:

    1. 将函数应用于 `Columns("A")´ 意味着所有列 无需使用 for 进行迭代。

    2. 在我的情况下,可能不是您的导入数字使用点小数分隔符,而我的系统使用逗号作为小数分隔符,因此导入 数字作为文本导入,“Max”和“Min”都不起作用 直到将点改为逗号。

    所以工作代码可能是:

    Private Sub CommandButton1_Click()
    
    Dim vMin, vMax
    
    Dim mg As Range
    
    Dim NOR, lastrow, currentrow As Long
    
    filetoopen = Application.GetOpenFilename("Text File (*.txt),*.txt", , "Select", , False)
    
            If VarType(filetoopen) = vbBoolean Then
            Exit Sub
            End If
            Workbooks.OpenText filetoopen, Origin _
            :=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
            , ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:= _
            False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1) _
            , Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
            Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1)), _
            TrailingMinusNumbers:=True
    
    'get number of rows (row with value inside)-------------
    
       With ActiveSheet
    
       NOR = .Cells(Rows.Count, "A").End(xlUp).Row
    
       End With
    
    'GET SMALLEST & LARGEST VALUE FROM COLUMN A==========
    
    With ActiveSheet
    
      lastrow = NOR
    
        'For currentrow = 2 To lastrow
    
        'Set mg = ThisWorkbook.Sheets(1).Rows(currentrow)
    
        'if row no data then no read------------------------
    
        'If WorksheetFunction.CountA(mg) = 0 Then
    
        'Else
    
            vMin = Application.WorksheetFunction.Min(Columns("A"))
    
            vMax = Application.WorksheetFunction.Max(Columns("A"))
    
         'End If
    
        'Next currentrow
    
    End With
    
    MsgBox "Minimum =  " & vMin & ", " & "Maximum =  " & vMax, vbInformation
    
    MsgBox "last row A is = " & NOR
    
    End Sub
    

    结果:

    希望对你有帮助!

    【讨论】:

    • 谢谢你的回答...我试了一下,但是为什么最小值和最大值都是0?
    • 不,最小值是 0.13,最大值是 0.25,记住我的电脑有逗号作为小数分隔符...如果有,记得点赞并标记为已回答。谢谢。
    • 对不起朋友,我很困惑..如何将点的代码替换为小数分隔符?谢谢你的帮助
    • 好吧,首先看看区域设置...检查您的系统是否配置为使用逗号“,”或点“。”作为小数分隔符(在我的情况下为逗号),然后作为您的来源使用“。”只需打开记事本并替换“。”对于“,”,并使用我对您的脚本所做的修改,它将起作用。
    【解决方案2】:

    这是一种替代方法,它将使用 ADO 直接读取文件,而不必将其导入 Excel,这样应该会更快。该选项是更少的代码,并且应该运行非常快,即使对于大型数据集也是如此。

    代码:

    Public Sub ShowMinAndMax()
        Dim objConnection As Object: Set objConnection = CreateObject("ADODB.Connection")
        Dim objRecordset  As Object: Set objRecordset = CreateObject("ADODB.Recordset")
        Dim FolderPath    As String: FolderPath = "C:\SomeFolderHere\" ' The folderpath to the file you want to read
    
        objConnection.Open "Provider=Microsoft.Ace.OLEDB.12.0;" & _
                           "Data Source=" & FolderPath & ";" & _
                           "Extended Properties=""text;HDR=No;FMT=TabDelimited"""
    
        'Get the minimum and maximum value, you also need to
        'change the fileName, currently my File is Named Example.Txt. 
        'You need to update that in the SQL statement
        objRecordset.Open "SELECT Min(F1) as MinVal, Max(F1) as MaxVal FROM Example.txt", objConnection, 3
    
        MsgBox ("The minimum value is: " & objRecordset.Fields("MinVal") & vbCrLf & _
                "The maximum value is: " & objRecordset.Fields("MaxVal"))
    
        'Clean Up
        If objRecordset.State = 1 Then objRecordset.Close
        objConnection.Close
        Set objConnection = Nothing
        Set objRecordset = Nothing
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2020-09-22
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多