Results 1 to 4 of 4

Thread: Excel - why doesn't this work?

  1. #1

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126

    Excel - why doesn't this work?

    Anyone tell me why the following code acts the way it does.

    I have a form that holds my print routines. The following code is stored in the form.

    shortened version of my code...
    [Highlight=VB]

    If optA.Value = True Then Set RngSelectedPrint = Sheets("One").Range("A1:k100")
    If optB.Value = True Then Set RngSelectedPrint = Sheets("Two").Range("A1", Range("a1").End(xlDown).End(xlToRight))
    RngSelectedPrint.Preview ' or RngSelectedPrint.PrintOut
    [Highlight=VB]

    I do have two option buttons on my form, one called optA, the other called optB.

    Problem is not the code doesn't word - it does. It just doesn't work consistently.
    From sheet One - I can select option A. Option B gives me a run time error #1004. Code never makes it past the if optB line.

    From sheet Two - I can select both options. They both work.

    Now, if optB didn't work at all, I would understand it. If my reference works once, why does it not work irrelevant of the sheet from which the form is called.
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    It sounds like sheet two is not initialized or something and that
    may be why when printing from sheet two the code works
    because sheet one is displayed first. Its not until sheet two is
    selected that the controls get initialized or set.

    Test this out after the workbook is opened by clicking on sheet
    two and back to sheet one and then print. If your code works
    then what I presented above is true.

    Also, changing your code to something like this may help.
    VB Code:
    1. If optA.Value = True Then
    2.     Set RngSelectedPrint = Sheets("One").Range("A1:k100")
    3. Else
    4.     Set RngSelectedPrint = Sheets("Two").Range("A1", Range("a1").End(xlDown).End(xlToRight))
    5. Endif
    6. RngSelectedPrint.Preview ' or RngSelectedPrint.PrintOut
    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
    Lively Member amer7862000's Avatar
    Join Date
    Apr 2004
    Location
    North West, UK
    Posts
    94
    try this:

    VB Code:
    1. If optA.Value = True Then
    2.     Set RngSelectedPrint = Sheets("One").Range("A1:k100")
    3. Else
    4.     Set RngSelectedPrint = Sheets("Two").Range("A1", Range("a1").End(xlDown).End(xlToRight))
    5. Endif
    6. RngSelectedPrint.PrintPreview

    "Through every dark night there's a brighter day, so no matter how hard it get, put your chest out and keep your head up, and handle it"

  4. #4

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    I think the problem lies with the xldown and xltoright parameters.

    I moved the code around, accessing sheet two prior to sheet one and still get the same error. It does not seem to let me use the xldown, xltoright parameters unless the form was launched from that particular sheet.

    And since this is a form, I can't use code like sheet(1).select since my focus is no longer on the workbook, but rather in the form itself.

    Any other suggestions/?
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

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