|
-
Apr 10th, 2004, 09:56 PM
#1
Thread Starter
Lively Member
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. ...
-
Apr 11th, 2004, 11:23 PM
#2
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:
If optA.Value = True Then
Set RngSelectedPrint = Sheets("One").Range("A1:k100")
Else
Set RngSelectedPrint = Sheets("Two").Range("A1", Range("a1").End(xlDown).End(xlToRight))
Endif
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 12th, 2004, 06:47 PM
#3
Lively Member
try this:
VB Code:
If optA.Value = True Then
Set RngSelectedPrint = Sheets("One").Range("A1:k100")
Else
Set RngSelectedPrint = Sheets("Two").Range("A1", Range("a1").End(xlDown).End(xlToRight))
Endif
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"
-
Apr 13th, 2004, 06:55 PM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|