【问题标题】:How to get the cells value through Function in VBA Excel如何通过VBA Excel中的函数获取单元格值
【发布时间】:2013-11-13 11:19:13
【问题描述】:

我正在处理一项任务,用户首先输入从“A”到“Z”的列,然后输入从 1 到 20 的行号。我必须编写一个函数 getIntData(col as String, row as Integer)作为整数返回活动工作表中该单元格的内容并将其显示在消息框中。如果数字是最大负整数,则显示带有感叹号的消息框。 函数 getIntData 返回一个整数值。如果单元格的内容不是数字,则返回最大可能的负值 (-2^15),即 -32768。

我已经为函数 getIntdata() 编写了一些代码,但它不能产生整数值。任何人都可以看看我错过了什么。另外,请指导我如何通过 Sub 程序运行函数。

 Sub getIn()

     Dim Row As Integer
     Dim Column As String
            Column = InputBox("Please enter the column letter from A-Z", "Column Letter")
            Row = InputBox("Please enter the row number from 1-20", "Row Number")
            Debug.Print getIntData(Column, Row)
 End Sub


Function getIntData(Col As String, Rw As Integer) As Integer
    Dim Result As Variant
      If Col = "" Or Rw < 1 Then
         Result = "Invalid inputs"
      ElseIf Not IsNumeric(Range(Col & Rw).Value) Then
         Result = -32768
      ElseIf Range(Col & Rw).Value <= -32768 Then
         Result = "!"
      ElseIf Range(Col & Rw).Value >= 32767 Then
         Result = "Now what?"
      Else
         Result = Range(Col & Rw).Value
      End If
      MsgBox Result
   ' return value to caller
         getIntData = Result
 End Function

【问题讨论】:

    标签: excel vba macos


    【解决方案1】:

    你需要getIntData = ...从函数返回一个值

    您还应该处理一些其他错误情况

    试试这个作为状态器

    Function getIntData(Col As String, Rw As Integer) As Integer
        Dim Result As Variant
        If Col = "" Or Rw < 1 Then
            Result = "Invalid inputs"
        ElseIf Not IsNumeric(Range(Col & Rw).Value) Then
            Result = -32768
        ElseIf Range(Col & Rw).Value <= -32768 Then
            Result = "!"
        ElseIf Range(Col & Rw).Value >= 32767 Then
            Result = "Now what?"
        Else
            Result = Range(Col & Rw).Value
        End If
        MsgBox Result
        ' return value to caller
        getIntData = CInt(Result)
    End Function
    

    从 Sub 尝试调用它

    Sub Demo()
        Debug.Print getIntData("A", 1)
    End Sub
    

    【讨论】:

    • 任务需求的Bcz我修改了你的子功能,请再看我的问题;但是在将此子函数与函数链接后,我在变量“Result”,“getIntData = Result”中收到错误“type mismatch error”,宏产生结果然后发生错误
    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多