ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 7481|回复: 14

十万火急--什么语句可以返回一个数组的维数?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2005-8-5 13:35 | 显示全部楼层 |阅读模式

TA的精华主题

TA的得分主题

发表于 2005-8-5 13:49 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

想返回一维还是二维〉〉

如果是这样,就免了

TA的精华主题

TA的得分主题

 楼主| 发表于 2005-8-5 14:32 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

Function CB_IndexCompute0() As Double Dim i, j, width As Integer Dim numerator, denominator, denominator_Pre, CB_Index_pre As Double 'Dim c Dim c As Range 'i Today 中CB的个数 'j 为转债余额表中CB的个数 'numerator为CB总市值 'denomintor为除数 'MsgBox "Cb_Index0000" i = 0 j = 0 CB_Index_pre = Worksheets("TODAY").Cells(13, 2).Value denominator_Pre = Worksheets("TODAY").Cells(13, 3).Value

For Each c In Worksheets("TODAY").[B2:BZ2] If c.Value <> "" Then i = i + 1 End If Next

'Dim CB_volumebook As String 'CB_volumebook = "D:\Index\转债余额表" 'CB_volumebook为转债余额表的路径 'Application.Workbooks.Open Filename:=CB_volumebook, updatelinks:=3 Workbooks("转债余额表").Activate For Each c In Worksheets("每日余额").[B2:BZ2] If c.Value <> "" Then j = j + 1 End If Next

If j = i Then ActiveWorkbook.Close Workbooks("最新").Worksheets("TODAY").Activate Dim Today_Volume(), Today_Price(), Yesterday_Volume(), Yesterday_Price() As Double ReDim Today_Volume(i - 1) ReDim Today_Price(i - 1) ReDim Yesterday_Volume(i - 1) ReDim Yesterday_Price(i - 1)

Dim p, q As Integer For q = 0 To i - 1 Step 1 Today_Volume(q) = Cells(5, q + 2).Value Today_Price(q) = Cells(8, q + 2).Value Yesterday_Volume(q) = Cells(6, q + 2).Value Yesterday_Price(q) = Cells(9, q + 2).Value Next

'判断有无存量的变化 'senario=0,存量没有变化 'senario=1,存量有变化 Dim senario As Integer 'Dim Index_Volume() As Integer

p = 0 For q = 0 To i - 1 Step 1 If Today_Volume(q) <> Yesterday_Volume(q) Then 'ReDim Index_Volume(p) 'Index_Volume(p) = q p = p + 1 End If Next

'Dim temp() As Double

If p <> 0 Then senario = 1 Else: senario = 0 End If

Select Case senario Case 0 numerator = 0 For q = 0 To i - 1 Step 1 If Today_Volume(q) > 0 Then numerator = numerator + Today_Volume(q) * Today_Price(q) / 100000000 End If Next CB_IndexCompute0 = numerator / denominator_Pre denominator = denominator_Pre

Case 1 Dim numerator_alternative As Double Dim check As Double numerator = 0 numerator_alternative = 0 For q = 0 To i - 1 Step 1 numerator = numerator + Yesterday_Volume(i) * Today_Price(i) / 100000000 numerator_alternative = numerator_alternative + Today_Volume(i) * Today_Price(i) / 100000000 Next denominator = denominator_Pre * numerator_alternative / numerator CB_IndexCompute0 = numerator / denominator_Pre check = numerator_alternative / denominator MsgBox check - CB_IndexCompute0

End Select 'Dim q As Integer 'For q = 1 To i Step 1 'If Cells(5, q).Value > 0 Then 'If Cells(8, i).Value = 0 Then 'Cells(8, i).Value = Cells(9, i).Value 'Cells(8, i).Font.Color = 3

'Compute0_Volume: 'Dim numerator_alternative As Double 'numerator = 0 'numerator_alternative = 0 'For q = 0 To i - 1 Step 1 'numerator = numerator + Yesterday_Volume(i) * Today_Price(i) / 100000000 'numerator_alternative = numerator_alternative + Today_Volume(i) * Today_Price(i) / 100000000 'Next 'denominator = denominator_Pre * numerator_alternative / numerator 'CB_IndexCompute0 = numerator / denominator_Pre 'Dim check As Double 'check = numerator_alternative / denominator 'GoTo Lastline

End If

End Function

不能返回值

不知道那个地方出错

TA的精华主题

TA的得分主题

发表于 2005-8-5 15:05 | 显示全部楼层

利用错误,检测维数

Public Function ArrayRange(mArray As Variant) As Integer Dim i As Integer Dim Ret As Integer Dim ErrF As Boolean ErrF = False On Error GoTo ErrHandle '判断代入的参数是否为数组 If Not IsArray(mArray) Then ArrayRange = -1 Exit Function End If 'VB中数组最大为60 For i = 1 To 60 '用UBound函数判断某一维的上界,如果大数组的实际维数时产生超出范围错误, ' 此时我们通过Resume Next 来捕捉错这个错误 Ret = UBound(mArray, i) If ErrF Then Exit For Next i '最后返回 ArrayRange = Ret Exit Function ErrHandle: Ret = i - 1 ErrF = True Resume Next End Function

Sub mm() Dim a(5, 5, 4, 3) MsgBox ArrayRange(a) End Sub

[此贴子已经被作者于2005-8-5 15:05:37编辑过]

TA的精华主题

TA的得分主题

发表于 2005-8-5 13:40 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2005-8-5 13:45 | 显示全部楼层
这2个是数组的下、上标了。不知道楼主是不是这个意思。

TA的精华主题

TA的得分主题

发表于 2011-10-4 23:41 | 显示全部楼层
6楼代码排版有问题啊。重新整理如下:
  1. Public Function ArrayRange(mArray As Variant) As Integer
  2. ' Written By : taller
  3. ' Declaer Date: 2005-08-05 15:05:07
  4. ’http://club.excelhome.net/forum.php?mod=viewthread&tid=114218
  5.     Dim i As Integer
  6.     Dim Ret As Integer
  7.     Dim ErrF As Boolean
  8.     ErrF = False
  9.    
  10.     On Error GoTo ErrHandle
  11.     If Not IsArray(mArray) Then '判断代入的参数是否为数组
  12.         ArrayRange = -1
  13.         Exit Function
  14.     End If
  15.    
  16.     'VB中数组最大为60
  17.     For i = 1 To 60
  18.     '用UBound函数判断某一维的上界,如果大数组的实际维数时产生超出范围错误,
  19.     ' 此时我们通过Resume Next 来捕捉错这个错误
  20.         Ret = UBound(mArray, i)
  21.         If ErrF Then Exit For
  22.     Next i
  23.     '最后返回
  24.     ArrayRange = Ret
  25.     Exit Function
  26. ErrHandle:
  27.     Ret = i - 1
  28.     ErrF = True
  29.     Resume Next
  30. End Function
  31. Sub mm()
  32.     Dim a(5, 5, 4, 3)
  33.     MsgBox ArrayRange(a)
  34. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2011-10-5 09:21 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  2. Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Var() As Any) As Long
  3. Function lsftest(a() As Variant) As Long
  4. Dim b As Long, c As Long, d As Long
  5. c = VarPtrArray(a)
  6. CopyMemory b, ByVal c, 4
  7. CopyMemory d, ByVal b, 2
  8. lsftest = d
  9. End Function
  10. Sub test()
  11. Dim e(9)
  12. Dim f(9, 9)
  13. Dim g(9, 9, 9)
  14. Debug.Print lsftest(e)
  15. Debug.Print lsftest(f)
  16. Debug.Print lsftest(g)
  17. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2011-10-5 10:11 | 显示全部楼层
本帖最后由 香川群子 于 2011-10-5 10:19 编辑

转Stanley的代码:
一个自定义函数Function ArrayDims()的用法:
和8楼的原理是一模一样的啦。

  1. Option Explicit
  2. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  3. Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
  4. Const AppName = "Stanley Pan Hint:"

  5. ' ************************************************************************
  6. ' Routine Name : (Public in Standard module) Sub ArrayDimensionTest
  7. ' Written By : Stanley
  8. ' Programmer : Stanley [ISOLA-J05Q6EYMB]
  9. ' Date Writen : 2006-03-30 20:01:07
  10. ' Inputs : N/A
  11. ' Outputs : N/A
  12. ' Description :
  13. ' :
  14. ' :
  15. ' Called By :
  16. ' ************************************************************************
  17. Public Function ArrayDims(ByVal lpArray As Long) As Integer
  18.     Dim lAddress As Long
  19.     CopyMemory lAddress, ByVal lpArray, 4
  20.     If lAddress = 0 Then
  21.     ' The array isn't initilized
  22.         ArrayDims = -1
  23.         Exit Function
  24.     End If
  25.     CopyMemory ArrayDims, ByVal lAddress, 2
  26. End Function
复制代码
下面是测试代码:
  1. Public Sub ArrayDimensionTest()
  2.     Dim varArray() As Integer 'dim a variables to contain array
  3.     ReDim varArray(1) As Integer 'one dimension
  4.     MsgBox ArrayDims(VarPtrArray(varArray)), vbInformation, AppName
  5.    
  6.     ReDim varArray(1, 2) As Integer 'two dimension
  7.     MsgBox ArrayDims(VarPtrArray(varArray)), vbInformation, AppName
  8.    
  9.     ReDim varArray(2, 3, 4) As Integer 'three dimension
  10.     MsgBox ArrayDims(VarPtrArray(varArray)), vbInformation, AppName
  11.    
  12.     ReDim varArray(1, 2, 3, 4) As Integer 'Four dimension
  13.     MsgBox ArrayDims(VarPtrArray(varArray)), vbInformation, AppName
  14. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2012-8-30 10:40 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-4 02:14 , Processed in 0.052282 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表