VBA Print Titles w/ Variable - wrong result

esseispercipi4

New Member
Joined
Apr 9, 2012
Messages
22
Sorry if this issue has already been addressed elsewhere; I couldn't find a thread anywhere.

I'm using VBA to set Print Titles to the row that the currently-selected cell is in. However, it keeps entering the wrong row into Print Titles (however, the MsgBox lines return the correct row number).

Code:
[INDENT=2][I]Sub Border_PrintArea()
[/I][/INDENT]
[INDENT=2][I]Dim activePrintRow As Integer[/I][/INDENT]
[INDENT=2][I]Dim activePrintColumn As Integer[/I][/INDENT]
[INDENT=2][I]activePrintRow = ActiveCell.Row[/I][/INDENT]
[INDENT=2][I]activePrintColumn = ActiveCell.Column[/I][/INDENT]
[INDENT=2][I]MsgBox activePrintRow
[/I][/INDENT]
[INDENT=2][I]    With ActiveSheet.PageSetup[/I][/INDENT]
[INDENT=2][I]
   MsgBox activePrintRow[/I][/INDENT]
[INDENT=2][I]        .PrintTitleRows = activePrintRow & ":" & activePrintRow[/I][/INDENT]
[INDENT=2][I]    End With[/I][/INDENT]
[INDENT=2][I]
  MsgBox activePrintRow
[/I][/INDENT]
[INDENT=2][I]End Sub[/I][/INDENT]

The other variable is used elsewhere in my code, but I've commented all of those lines out to see if I was somehow changing the variable, so now these are the only active lines of code.
If the user is in row 1, then the Print Titles is set at 2:2. If the user is in row 2, then the Print Titles is set at 3:3. If the user is in row 4, then the Print Titles is set at 7:7. If the user is in row 7, then the Print Titles is set at 13:13 (this is the same in every file I've run the macro in).

I'm at a loss as to how the MsgBox displays the correct value each time, but the Print Titles contains a different value.

Any help would be greatly appreciated. Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I have figured out what was happening, but not why it was happening.

It seems that the PrintTitleRows code was incrementing the variable for some reason. If I actually hardcode
Code:
.PrintTitleRows = 1 & ":" & 1
then it gives the correct result in the Print Titles box of using the row that was currently selected (although I would have expected that code to give a print title of row 1, regardless of what row you were in).
It appears the code takes the selected row, subtracts one, and then adds whatever you put in the VBA code. Before, if the row was 4, it was taking 4-1+[4] for a row of 7. Now, it is taking 4-1+[1] for a row of 4.

However, I have no idea why this is happening. If anyone has any insight into this, it would be greatly appreciated.
 
Upvote 0
I have figured out what was happening, but not why it was happening.

It seems that the PrintTitleRows code was incrementing the variable for some reason. If I actually hardcode
Code:
.PrintTitleRows = 1 & ":" & 1
then it gives the correct result in the Print Titles box of using the row that was currently selected (although I would have expected that code to give a print title of row 1, regardless of what row you were in).
It appears the code takes the selected row, subtracts one, and then adds whatever you put in the VBA code. Before, if the row was 4, it was taking 4-1+[4] for a row of 7. Now, it is taking 4-1+[1] for a row of 4.

However, I have no idea why this is happening. If anyone has any insight into this, it would be greatly appreciated.

Try this

Code:
[I]        .PrintTitleRows = [B]"$"[/B] & activePrintRow & ":" & [B]"$"[/B] & activePrintRow[/I]
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top