Results 1 to 7 of 7

Thread: [Resolved]Excel Index Function

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    Resolved [Resolved]Excel Index Function

    In addition to the other issue I'm have with ranges in the worksheet function Index that I posted in the thread "Excel Ranges," I have run into another problem which I can't figure out how to work around. I have to use an index on a block of cells, say B2:AL38, which has some numbers scattered in it, but a lot of blank cells as well.
    The blank cells must remain blank.
    When i use :
    Application.Index([Sheet1!B2:AL38], row, col)
    and it sees a blank cell, it gives me an object mismatch--- since it finds a string and wants a value---I think, at least. How can I make it think "zero" when it sees a blank cell?
    How could I just do a :
    If Application.Index(Inputs)='object mismatch' Then
    Var=Var+0
    Else
    Var=Var+Application.Index(Inputs)
    End If
    ?
    How would I really write that?
    Sigh.

    Thanks for your help!
    Last edited by Bartender; Jul 13th, 2005 at 06:45 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Index Function

    If Application.Index(Inputs)='object mismatch' Then

    Would be trappable by using an error handling routine. Find the error number for that error and trap it like so.

    VB Code:
    1. Private Sub Something()
    2.  
    3.     On Error goto MyError
    4.  
    5.     'Blah, blah, blah....
    6.  
    7.     Exit Sub
    8. MyError:
    9.     If err.number = xxx Then
    10.         'Something
    11.     Else
    12.         'Somethingelse
    13.     End If
    14. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    Re: Excel Index Function

    Thanks, I'll give that a try--put it at the end of my loop.
    Thanks.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    Re: Excel Index Function

    Rob,
    Quick question:
    If I put a a couple On Error GoTo MyError's like so:
    VB Code:
    1. For ii = 1 to Size
    2. On Error GoTo MyError
    3. 'error could happen here each loop
    4. MyError:
    5. 'stuff
    6. Next ii
    7. For ii = 1 to Size
    8. On Error GoTo MyError2
    9. 'Error can happen each loop
    10. MyError2:
    11. 'stuff
    12. Next ii
    Can it recognize the end of one On Error statement and see the start of the next?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Index Function

    Its better to have your error handling in one place at the bottom and its not really used like a goto or gosub statement.
    VB Code:
    1. On Error GoTo MyError
    2. For ii = 1 to Size
    3.     'error could happen here each loop
    4. Next ii
    5.  
    6. On Error GoTo MyError2
    7. For ii = 1 to Size
    8.     'Error can happen each loop
    9. Next ii
    10.  
    11. On Error GoTo 0 'Turn off error handling.
    12. 'Do something
    13.  
    14. On Error GoTo MyError 'Turn it back on
    15. 'do something
    16.  
    17. exit sub
    18.  
    19. MyError:
    20. 'stuff
    21. exit sub
    22.  
    23. MyError2:
    24. 'stuff
    25. exit sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    Re: Excel Index Function

    Rob,
    If I have an error, I want the loop to still continue. The error may recur, or it may not. In the even of an error, I just want to add 0 to my running total and continue as if nothing had happened.
    VB Code:
    1. For ii = 1 to 6
    2. Val = Val + Application.Index(Inputs)
    3. Next ii
    If it went through errors, Val might end up containing "5 + 3 + Error + 2 + Error + 11"
    So I would want it to read:
    "5+3+0+2+0+11" = 21

    But, you implied that it is wrong to say, for instance:
    VB Code:
    1. For ii = 1 to 6
    2. On Error GoTo MyError
    3. Val = Val +Application.Index(Inputs)
    4. MyError:
    5. Val = Val + 1
    6. Next ii

    I see how the myerror block might interrupt the code there, I'm not sure how it works...
    Is it that the Loop wouldn't see the Next, since it would be 'part of' the MyError?

    Theres no way to contain a MyError statement
    MyError:
    'Stuff
    /MyError

    ...or something like that?

    Thanks for helping.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel Index Function

    Trap for error 13 - Type Mismatch error. Then do a Resume Next in the handler for that error.
    VB Code:
    1. On Error GoTo MyError
    2. For ii = 1 to Size
    3.     'error could happen here each loop
    4. Next ii
    5.  
    6. On Error GoTo MyError2
    7. For ii = 1 to Size
    8.     'Error can happen each loop
    9. Next ii
    10.  
    11. On Error GoTo 0 'Turn off error handling.
    12. 'Do something
    13.  
    14. On Error GoTo MyError 'Turn it back on
    15. 'do something
    16.  
    17. exit sub
    18.  
    19. MyError:
    20.     If err.Number = 13 then
    21.         resume next
    22.     else
    23.         msgbox err.number & " - " & err.description
    24.     Endif
    25. exit sub
    26.  
    27. MyError2:
    28. 'stuff
    29. exit sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width