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!

Problem with Excel page breaks

Status
Not open for further replies.

Stoffel24

Technical User
Apr 4, 2002
121
ZA
Hi all
I have a bizarre problem. The following code runs fine on most machines but on a few, it gives me a subscript out of range error at the line that is red. I added the following watch to see what value was coming through: "Worksheets("Tensile-Bend").HPageBreaks.Count"
This showed me that on the machine where there is no problem, this watch remains constant at 8. However, on the machines where the code doesn't work, the value changes, starting initially at 15. The values in the square brackets represent the value of the watch on the machines where the code works and where it doesn't respectively. Notice how it goes 15, 17 and then critically and inexplicably, 1!

Sub PageSetup()
'** Here we manually set a page break and place column headings in.
Dim Rw As Integer
Dim Found As Boolean, Cnter As Integer, Cnter2 As Integer
Dim Rwnext As Integer

ActiveWindow.View = xlPageBreakPreview '[8, 15]
For Cnter = 1 To Worksheets("Tensile-Bend").HPageBreaks.Count '[8, 17]
Found = False '[8, 1]
Range(Worksheets("Tensile-Bend").HPageBreaks(Cnter).Location.Address).Select

'** Do a whole bunch of things here.

Next
ActiveWindow.View = xlNormalView
End Sub

Because Excel determines there is only one page break AFTER the "For Cnter = ..." step, when we get to the loop where Cnter = 2 the line "Range(Worksheets("Tensile-Bend").HPageBreaks(Cnter).Location.Address).Select" gives us Subscript out of range message. I have tried putting in a line like "BrkCnt = Worksheets("Tensile-Bend").HPageBreaks.Count" after the "For Cnter = ..." line but this seems to have no effect. [Ponder] "Come on Scott throw me a frikin' bone here."
 
This may be to do with screen resolution. Being as the code works on some machines and not others, it seems to me that unless the machines are on a different OS, it is something to do with the machine rather then the code. I have noticed that excel (in earlier versions anyway) seems to set the pagebreaks with some degree of respect to the screen resolution. However, the fact that the hpb count changes like that is bizarre indeed. I might expect there to be different initial numbers of hpbs but not for it to change as you step thru the code. If you want to email me a copy of the workbook, I'd be happy to have a look and see if there is anything I can find that might be causing this.
Geoff.Barraclough@Punchpubs.co.uk Rgds
Geoff
"Some cause happiness wherever they go; others whenever they go."
-Oscar Wilde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top