标题:请教关于VB读取excel列的最大值与最小值!
取消只看楼主
事业男儿
Rank: 2
等 级:论坛游民
帖 子:308
专家分:14
注 册:2007-4-25
结帖率:81.69%
已结贴  问题点数:20 回复次数:2 
请教关于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


搜索更多相关主题的帖子: 读取 最大值 Path String If 
2018-04-04 14:24
事业男儿
Rank: 2
等 级:论坛游民
帖 子:308
专家分:14
注 册:2007-4-25
得分:0 
请问版主,这个代码在运行过程中修改excel的裂和行怎么不管用,甚至显示最大值与最小的控件都会消失,这是怎么回事呢!
Private Sub Command1_Click()
  Dim max1, min1
  Call Load_execl("D:\1.xlsx", Text1.Text, max1, min1)
  Label1.Caption = max1
  Label2.Caption = min1
 End Sub

 Public Sub Load_execl(execl_name, ByVal column1 As String, max1, min1)
   Dim xlapp As Excel.Application
   Dim xlBook As Excel.Workbook
   Dim xlSheet As Excel.Worksheet
   Dim rows, temp
   Set xlapp = CreateObject("Excel.Application")
   xlapp.Visible = False
   Set xlBook = xlapp.Workbooks.Open(execl_name)
   Set xlSheet = xlBook.Worksheets(1)
   rows = xlSheet.UsedRange.rows.Count '【行数】
  temp = column1 & Text2.Text & column1 & Trim(Str(rows))
 On Error Resume Next
   max1 = xlSheet.Application.Max(Range(temp))
   min1 = xlSheet.Application.Min(Range(temp))
   xlBook.Close (True)
   xlapp.Quit
   Set xlapp = Nothing
 End Sub
2018-04-05 02:42
事业男儿
Rank: 2
等 级:论坛游民
帖 子:308
专家分:14
注 册:2007-4-25
得分:0 
问题终于搞定,谢谢wds1版主,耐心的结束!
2018-04-05 13:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-485764-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.116309 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved