0

I am working on a task where the users enters first a column from “A” to “Z” and then a row number from 1 to 20. I have to write a function getIntData(col as String, row as Integer) as Integer that returns the content of that cell in the active worksheet and shows it in a message box. If the number is the maximum negative Integer show instead a message box with an exclamation mark. The function getIntData returns an Integer value. If the content of the cell is not numeric then return the maximum possible negative value (-2^15) i.e. -32768.

I have write some code for the function getIntdata() but it cann't produce integer value. Can anyone please look what I am missing. Moreover, please guide me how to run function through Sub procedure.

 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
4

1 回答 1

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
于 2013-11-13T11:32:59.607 回答