【发布时间】: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