Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code works on some machines and not others

Status
Not open for further replies.

Stoffel24

Technical User
Apr 4, 2002
121
ZA
Hi again
I have a problem with some code I wrote working fine on my and several other PCs but not working on a few other isolated PCs. I have had a look and the references are all the same on both the working and non working PCs.
VBA, Excel 9.0 Object Library, OLE Automation and MS Office 9.0 Object library.

Can anyone suggest any other areas I should check.

Below is the code sample:

For Cnter = 1 To Worksheets("Hard-Micro").HPageBreaks.Count
Found = False
Range(Worksheets(1).HPageBreaks(Cnter).Location.Address).Select
'This is where I get a subscript out of range problem
Next

Thanks very much.
 
Does the error happens immediately (Cnter=1) or at some particular point in the loop?
Rob
[flowerface]
 
It happens immediately on the first time it goes into the loop. The strange thing is that I ammended the code as follows:
Msgbox Worksheets("Hard-Micro").HPageBreaks.Count 'ans = 5
For Cnter = 1 To Worksheets("Hard-Micro").HPageBreaks.Count
Found = False
Msgbox Worksheets("Hard-Micro").HPageBreaks.Count 'ans = 0
Range(Worksheets(1).HPageBreaks(Cnter).Location.Address).Select
'This is where I get a subscript out of range problem
Next

As you see, my 2 message boxes give me a result of 5 and then 0! I am totally confused.
 
I think that pasgebreak code is pretty flawed in general. One thing I have found is that it helps to be in the pagebreakpreview mode BEFORE executing any pagebreak code
Try wrapping your code in this:


ActiveWindow.View = xlPageBreakPreview
'code
ActiveWindow.View = xlNormalView

If you set screenupdating = false, this won't show Rgds
Geoff
"Some cause happiness wherever they go; others whenever they go."
-Oscar Wilde
 
Thanks Geoff. I actually have done this. I was having even more problems with this code until I put the lines you suggested in. I keep wondering whether it has something to do with an excel option or a reference or such a thing, since it is working on some pcs. This indicates to me that it is a problem with the PC rather than the code.
 
That's just too strange. What happens if you duplicate the first msgbox statement, i.e.

Msgbox Worksheets("Hard-Micro").HPageBreaks.Count 'ans = 5
Msgbox Worksheets("Hard-Micro").HPageBreaks.Count 'ans = 5
For Cnter = 1 To Worksheets("Hard-Micro").HPageBreaks.Count
Found = False
Msgbox Worksheets("Hard-Micro").HPageBreaks.Count 'ans = 0
Range(Worksheets(1).HPageBreaks(Cnter).Location.Address).Select
'This is where I get a subscript out of range problem
Next

Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top