请教关于VB读取excel列的最大值与最小值!
在工作中总要看看数据最大值与最小值,然而每次用公式感觉很麻烦,而且行数又多,希望用VB来做个软件,随时读取比较方便,以下代码在网上找到的读取最大值,但是我想同时读取最大值与最小值所以在上面加了很多代码,功能是实现了,但是觉得声明栏里的代码,可以很精简的,就是不知道怎么写,还有一个就是我想在界面多画一个Text.Text,用于输入不同的列,A.B.C......请各位老师指点一下Function GetMinValueFromColumn(ByVal Path As String, ByVal SheetName As String, ByVal RangeName As String) As Variant
Dim xlWb As Object
On Error Resume Next
Set xlWb = GetObject(Path)
If Err.Number = 0 Then
GetMinValueFromColumn = xlWb.Application.WorksheetFunction.Min(xlWb.Sheets(SheetName).Range(RangeName))
Else
GetMinValueFromColumn = "Error"
End If
If Not (xlWb Is Nothing) Then Set xlWb = Nothing
End Function
'.................................................................................................................................
Function GetMaxValueFromColumn(ByVal Path As String, ByVal SheetName As String, ByVal RangeName As String) As Variant
Dim xlWb As Object
On Error Resume Next
Set xlWb = GetObject(Path)
If Err.Number = 0 Then
GetMaxValueFromColumn = xlWb.Application.WorksheetFunction.Max(xlWb.Sheets(SheetName).Range(RangeName))
Else
GetMaxValueFromColumn = "Error"
End If
If Not (xlWb Is Nothing) Then Set xlWb = Nothing
End Function
Private Sub Command1_Click()
Text1.Text = GetMinValueFromColumn(App.Path & "\Test.xlsx", "Sheet1", "A1:A8")
Text2.Text = GetMaxValueFromColumn(App.Path & "\Test.xlsx", "Sheet1", "A1:A8")
End Sub