我的代码存在问题,它无法显示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
答案 0 :(得分:0)
我已经尝试过你的代码......两个人认为:
将函数应用于`Columns(“A”)'意味着所有列都是如此 没有必要迭代for。
在我的情况下,可能不是你的导入数字使用点小数分隔符,我的系统使用逗号作为小数分隔符,所以导入 数字作为文本导入,“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
结果:
希望它有所帮助!
答案 1 :(得分: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