Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    May 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling a Sub with OnKey and sending an array (Excel 2003)

    I have a procedure that has two parameters, both of which are arrays. I want the procedure to be called after somebody has pressed the escape key. Currently I'm using OnKey to wait for the input. I can get the OnKey to work if I'm sending it an integer or an string, but not if I'm sending an array. Any suggestions on how to do this, or if there is something better then OnKey to use.
    The code I have looks like this:

    Public Sub Main()

    Dim Block(Board_Height, Board_Width) As Shape
    Dim IA_Block(Board_Height, Board_Width) As String

    Application.OnKey "{Esc}", "'End_Main IA_Block, Block'"

    End Sub
    Private Sub End_Main(ByRef IA_Block, ByRef Block)

    Dim Block(Board_Height, Board_Width) As Shape
    Dim IA_Block(Board_Height, Board_Width) As String

    For i = 0 To UBound(IA_Block, 1) - 1 Step 1
    For j = 0 To UBound(IA_Block, 2) - 1 Step 1
    IA_Block(i, j) = "I"
    Block(i, j).Delete
    Next
    Next
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling a Sub with OnKey and sending an array (Excel 2003)

    You have declared Block and IA_Block both in Main and in End_Main. You should declare them at the top of the module. And End_Main should be public, not private:

    Public Block(Board_Height, Board_Width) As Shape
    Public IA_Block(Board_Height, Board_Width) As String

    Public Sub Main()
    Application.OnKey "{Esc}", "'End_Main IA_Block, Block'"
    End Sub

    Public Sub End_Main(ByRef IA_Block, ByRef Block)
    Dim i As Integer
    Dim j As Integer
    For i = 0 To UBound(IA_Block, 1) - 1 Step 1
    For j = 0 To UBound(IA_Block, 2) - 1 Step 1
    IA_Block(i, j) = "I"
    Block(i, j).Delete
    Next
    Next
    End Sub

  3. #3
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Calling a Sub with OnKey and sending an array (Excel 2003)

    I don't understand what you are trying to do.
    In the first code:
    What are the const variables:
    Board_Height
    Board_Width

    In the second code
    Why do you DIM the arrays:
    Block, IA_Block
    when you are passing them to the procedure. You are trying to declare them when they are already declared
    Also, I think it should not be private or the "Exc" can not run the code...

    Where are you creating and storing the arrays:
    Block, IA_Block

    Your first code dims 2 arrays. and sets the onkey up.

    Then afterwards, when you press <ESC> you want to call the 2nd and send the 2 arrays. Where are they? you never create them in code. Also even if they were created in code, after the code is run the variables are "cleared" unless you declare them as public...

    Could you elaborate on what you are trying to do? To do what I think you are asking, requires declaring the variables as PUBLIC (outside a procedure), filling the arrays, then running the onkey.

    Then when <esc> is pressed the variables will be created and not be empty when they are passed to the procedure...

    Steve

  4. #4
    New Lounger
    Join Date
    May 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling a Sub with OnKey and sending an array (Excel 2003)

    I was hoping to avoid switching stuff to public. But if that is the only way that VB can handle it, then i will have to

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •