首页 > 解决方案 > 为什么类型转换不能解决这个 VBA 溢出错误

问题描述

我已经阅读了许多关于 VBA 溢出错误的帖子,但仍然遇到问题。寻求帮助。以下代码片段中的最后一行生成溢出错误。我尝试了很多类型转换,但没有任何效果。

我怀疑的是这在很长一段时间内都很好用。突然之间,它开始失败。可能是由于其他地方发生了变化。我正在努力查看我是否只是有编码错误,或者我的程序是否以某种方式破坏了内存。是否有可能我以错误的方式使用用户类型?我已经使用 VBA 很长时间了,但是自从我使用用户类型以来已经有一段时间了。

最后,我可以在有问题的代码行之前放置一个断点,并在即时窗口中使用 debug.print 执行这个确切的代码。它工作正常。进入代码失败。

希望您能提供任何帮助。谢谢。

顺便说一句,我在 Mac 上运行 MSOffice。

Sub create_3d_data()  
  
  Dim iPt As Integer ' point counter
  Dim iPoly As Integer ' poly counter
  Dim iKeystone As Integer 'keystone counter
  Dim theta As Single 'segment angle as we sweep the ring circle
  Dim segment_angle As Single, temp As Single
  
  iPt = 0: iPoly = 0: iKeystone = 0
  
  For iRing = 0 To user_ring_count - 1
    temp = CSng(User_Rings(iRing).number_of_segments)
    segment_angle = 3.14159 * 2# / temp                <----- Overflow Error

以下是一些其他定义代码片段:

'in a separate Module in this VBAProject
Type User_Ring
  bottom_elevation As Single
  thickness As Single
  inside_radius As Single
  outside_radius As Single
  number_of_segments As Integer
  offset As Single
  edge_length As Single
  pattern As String
  spacer_size As Single
  spacer_color As Long
End Type

'in the Declarations section of the same workbook as the troubling code above
Dim User_Segments(10) As User_Segment
Dim User_Rings(30) As User_Ring
Dim user_ring_count as Integer

标签: excelvbaoverflow

解决方案


推荐阅读