Results 1 to 6 of 6

Thread: subscript out of range?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    27

    subscript out of range?

    I've got a piece of code that adds the values in cells J12:J30 together and gives the total. In theory this should work:

    Set Comm = Worksheets("Sheet2").Range("J12:J50")
    showComm = Application.WorksheetFunction.Sum(Comm)
    MsgBox showComm

    I keep getting a "Subscript out of range" error. Any ideas?

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

    Re: subscript out of range?

    I tried your code with and with data in the range and I got no errors, only a sum with data and a 0 for no data.
    Do you have a sheet named "Sheet2"?
    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
    Junior Member
    Join Date
    Apr 2002
    Posts
    27

    Re: subscript out of range?

    Hi RobbDogg,
    Yeah, Ive got a sheet called Sheet2 on the IDE it shows as Sheet2(Pipeline), and out of interest i tried using 'Pipeline' instead of 'Sheet2' but it didnt make a difference. I cant understand it myself, it SHOULD work

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

    Re: subscript out of range?

    Try by index number instead.
    VB Code:
    1. Set Comm = Worksheets(1).Range("J12:J50")
    2. 'Or
    3. Debug.Print Worksheets.Count
    The count will tell you how many indices there are in the workbook. Then use the .Name property to
    see which sheet it is referring to.
    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    27

    Resolved Re: subscript out of range?

    changed it to
    Set Comm = Worksheets(2).Range("J12:J50")

    its working now cheers!

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

    Re: subscript out of range?

    If you Debug.Print Worksheets(2).Name and you get a pipe char at the end, you still may be able to ref the sheet
    by its name if you try adding a $ sign after "Sheet2$" in code. The pipe may represent something special of the sheet. Or try renaming
    the sheet to something else as a test.
    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