2016-10-02 118 views
0

我有关于我的代码的问题,它不能显示列A中最小和最大的值。我不知道丢失或错误代码在哪里...如何通过VBA在特定工作表的列中获得最小(最小)和最大(最大)值

我的程序打开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

回答

0

我试过你的代码...和两个认为:

  1. 将函数应用于`栏( “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 

而且结果:

enter image description here

希望它能帮助!

+0

谢谢你的回答......我试试吧,但为什么最小值和最大值为0? – aaf

+0

不,最小值为0.13,最大值为0.25,请记住我的电脑有逗号作为小数分隔符......如果存在,请记住upvote并标记为已回答。谢谢。 – ZeroWorks

+0

对不起,朋友,我迷茫..如何将点代码替换为小数点分隔符?感谢您的帮助 – aaf

0

这是一种替代方法,它将使用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