【问题标题】:For loop is not doing the next part or something is wrong with my function VBAFor 循环没有做下一部分,或者我的函数 VBA 有问题
【发布时间】:2014-10-21 18:42:52
【问题描述】:

使用罗马数字计算器(仍在学习,所以如果这是一个简单的解决方法,请不要判断:P)

我遇到的问题是,当查看是否可以使用 Debug.print 从函数 (arr_rnnum) 中的数组中读取值时,第一个值可以正常读取,但第二个值始终返回零。我尝试在 for 循环中添加计数 j = j + 1 但这没有效果

例如,如果我将 debug.print 更改为 getting_values(0) 并在输入框中输入 X,V 它将打印 10 但如果我将其更改为 debug.print 到 getting_values(1) 它会打印 0

如果有人能看出问题所在,那将是一个很大的帮助

这是我的代码

Public Sub main()
   Debug.Print getting_values(1)
End Sub

Function getting_values()    
   Dim arr_rnstr() As String
   Dim rnstr As String
   Dim arr_rnnum() As Integer

   rnstr = InputBox("enter the roman numeral in descending order of value and put a comma between each")
   arr_rnstr() = Split(rnstr, ",") 'splits the string the user entered into single values'
   ReDim arr_rnnum(UBound(arr_rnstr)) 'sets the size of the array of numbers to the upper bound of the array of letter the user entered'
   For j = LBound(arr_rnstr) To UBound(arr_rnstr) 'this section checks each value of the array of letters and replaces it with a number in a second array'
      Select Case arr_rnstr(j)
         Case Is = "I"
            arr_rnnum(j) = 1
         Case Is = "V"
            arr_rnnum(j) = 5
         Case Is = "X"
            arr_rnnum(j) = 10
         Case Is = "L"
            arr_rnnum(j) = 50
         Case Is = "C"
            arr_rnnum(j) = 100
         Case Is = "D"
            arr_rnnum(j) = 500
         Case Is = "M"
            arr_rnnum(j) = 1000
      End Select
      j = j + 1
   Next j
   getting_values = arr_rnnum
End Function

【问题讨论】:

  • 我在您的函数/方法getting_values 中没有看到参数。代表?尝试getting_values()(1),因为那将是一个方法调用,然后是返回数组的索引..我想..我在 VBA 中不是那么强
  • @Brett 感谢您的回复,但当您说您没有看到写入的参数或参数 arg(两周前才开始编码)时,我不明白您的意思。 0 和 1 表示arr_rnnum 数组中的第一个和第二个值。我试过getting_values()(1) 但没用
  • 罗杰。但在实际调用该方法之前它不会是数组类型。所以getting_values(0) 不是 获取结果数组的0 索引,它正在调用该方法并将0false.. 的值传递给它

标签: arrays vba function printing


【解决方案1】:

答案是你需要在使用结果数组之前完成方法调用..

改变

Debug.Print getting_values(1)

Debug.Print getting_values()(1)

您当前调用getting_values 方法的值01 分别代表falsetrue

我不知道为什么在您的方法调用中传递 false 会允许 Debug Print 显示结果数组的第一个值。


Public Sub main()
   Debug.Print getting_values()(1)
End Sub

Function getting_values() As Integer()
    Dim arr_rnstr() As String
    Dim rnstr As String
    Dim arr_rnnum() As Integer

    rnstr = InputBox("enter the roman numeral in descending order of value and put a comma between each")
    ' split the string the user entered into single values
    arr_rnstr() = Split(rnstr, ",") 

    ' set the size of the array of numbers to the upper bound of the array of letter the user entered
    ReDim arr_rnnum(UBound(arr_rnstr)) 

    ' checks each value of the array of letters and replaces it with a number in a second array
    For index = LBound(arr_rnstr) To UBound(arr_rnstr) Step 1           
        Select Case arr_rnstr(index)
            Case Is = "I"
                arr_rnnum(index) = 1
            Case Is = "V"
                arr_rnnum(index) = 5
            Case Is = "X"
                arr_rnnum(index) = 10
            Case Is = "L"
                arr_rnnum(index) = 50
            Case Is = "C"
                arr_rnnum(index) = 100
            Case Is = "D"
                arr_rnnum(index) = 500
            Case Is = "M"
                arr_rnnum(index) = 1000
        End Select
    Loop
    getting_values = arr_rnnum
End Function

【讨论】:

  • 刚刚在使用 X,V Debug.Print getting_values()(0) 时再次尝试它仍然返回第一个值(10)但是当 Debug.Print getting_values()(1) 它返回 0(应该是 5)
  • getting_values()(0) 应该返回结果数组的第一个(零)索引......正如预期的那样......确保您使用逗号分隔输入;确保您删除 j = j + 1 行.. 继续并指定 for 循环中的步骤以及 For j = LBound(arr_rnstr) To UBound(arr_rnstr) Step 1
  • 最后但同样重要的是,Explicit 使用您的函数返回类型.. 使用 integer() 的返回类型声明函数
【解决方案2】:

我想我已经解决了你的问题,你不能像这样返回数组,因为它不返回任何东西。存储到一个字符串并返回它,这样会更容易。

Public Sub main()
   Debug.Print getting_values()
End Sub


Function getting_values()
   Dim arr_rnstr() As String
   Dim rnstr As String
   Dim arr_rnnum() As Integer
   Dim arr_num(12) As String
   Dim rn As String

   rnstr = InputBox("enter the roman numeral in descending order of value and put a comma between each")
   arr_rnstr() = Split(rnstr, ",") 'splits the string the user entered into single values'
   ReDim arr_rnnum(UBound(arr_rnstr)) 'sets the size of the array of numbers to the upper bound of the array of letter the user entered'
   For j = LBound(arr_rnstr) To UBound(arr_rnstr) 'this section checks each value of the array of letters and replaces it with a number in a second array'
      Select Case arr_rnstr(j)
         Case Is = "I"
              arr_num(j) = 1
         Case Is = "V"
              arr_num(j) = 5
         Case Is = "X"
              arr_num(j) = 10
         Case Is = "L"
              arr_num(j) = 50
         Case Is = "C"
              arr_num(j) = 100
         Case Is = "D"
              arr_num(j) = 500
         Case Is = "M"
              arr_num(j) = 1000
      End Select
      rn = rn + arr_num(j) + " "
   Next j
   getting_values = rn
End Function

【讨论】:

    猜你喜欢
    • 2018-03-14
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    相关资源
    最近更新 更多