Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
RJH RJH is offline
external usenet poster
 
Posts: 44
Default Active sheet?

I'm still very new to this stuff and I'm having a problem detecting the
active sheet. All I seem to accomplish is to activate sheet 2.
Obviously I'm using the wrong code here. I'm trying to detect which of 2
sheets (2 or 3) is active and print the correct header for that sheet. I'm
not even sure the else statement will work, I haven't got it to go that far!
Any light you can shed on this would be great.

Thanks!

RJH

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Worksheets("Sheet2").Activate Then
Dim wkSht As Worksheet
For Each wkSht In ActiveWindow.SelectedSheets
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2"))
End With
Next wkSht

Else
For Each wkSht In ActiveWindow.SelectedSheets
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2") & "Totals")
End With
Next wkSht
End If
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Active sheet?

Hi there

Use the predefined object ActiveSheet to access the currently active
sheet. So, instead of [If Worksheets("Sheet2").Activate Then] use
something like [If ActiveSheet.Name = "Sheet2" Then].

However, I noticed that you have almost identical code in your
if- and else-branches. I suggest to use the following modification
of your code to prevent duplicates (not tested):

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim wkSht As Worksheet
For Each wkSht In ActiveWindow.SelectedSheets
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2"))
' Here's the new code:
If ActiveSheet.Name = "Sheet2" Then
' append the string "Totals" to the center header
.CenterHeader = .CenterHeader & "Totals"
End If
End With
Next wkSht
End Sub


Cheers,
Martin



"RJH" schrieb im Newsbeitrag
...
I'm still very new to this stuff and I'm having a problem detecting the
active sheet. All I seem to accomplish is to activate sheet 2.
Obviously I'm using the wrong code here. I'm trying to detect which of 2
sheets (2 or 3) is active and print the correct header for that sheet.

I'm
not even sure the else statement will work, I haven't got it to go that

far!
Any light you can shed on this would be great.

Thanks!

RJH

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Worksheets("Sheet2").Activate Then
Dim wkSht As Worksheet
For Each wkSht In ActiveWindow.SelectedSheets
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2"))
End With
Next wkSht

Else
For Each wkSht In ActiveWindow.SelectedSheets
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2") &

"Totals")
End With
Next wkSht
End If
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Active sheet?


For Each wkSht In ActiveWindow.SelectedSheets
Select Case wkSht.Name
Case "Sheet2"
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2"))
End With
Case "Sheet3"
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2") & "Totals")
End With
End Select
Next wkSht



"RJH" wrote in message
...
I'm still very new to this stuff and I'm having a problem detecting the
active sheet. All I seem to accomplish is to activate sheet 2.
Obviously I'm using the wrong code here. I'm trying to detect which of 2
sheets (2 or 3) is active and print the correct header for that sheet.

I'm
not even sure the else statement will work, I haven't got it to go that

far!
Any light you can shed on this would be great.

Thanks!

RJH

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Worksheets("Sheet2").Activate Then
Dim wkSht As Worksheet
For Each wkSht In ActiveWindow.SelectedSheets
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2"))
End With
Next wkSht

Else
For Each wkSht In ActiveWindow.SelectedSheets
With wkSht.PageSetup
.CenterFooter = "&12 " & _
Application.UserName & ", " _
& Format(Now(), "mmmm-dd-yyyy") & " &T"
.CenterHeader = "&20 &B" & "" _
& Format("Credit Card Reconciliation Statement" & Chr(10) _
& "Billing Date " & Worksheets("Sheet2").Range("M2") &

"Totals")
End With
Next wkSht
End If
End Sub




  #4   Report Post  
Posted to microsoft.public.excel.programming
RJH RJH is offline
external usenet poster
 
Posts: 44
Default Active sheet?

Thanks for your help!

RJH


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy sheet and make new sheet active belvy123 Excel Discussion (Misc queries) 5 April 24th 08 03:33 PM
Active Cell Copy And Paste Sheet to Sheet A.R.J Allan Jefferys New Users to Excel 4 May 4th 06 02:04 AM
Active sheet Michael Excel Discussion (Misc queries) 5 June 27th 05 12:56 PM
Using the Active cell in one sheet for another sheet JOHNNY_E Excel Discussion (Misc queries) 0 May 4th 05 06:19 AM
How to make a sheet the active sheet? [email protected] Excel Programming 1 October 26th 03 12:25 AM


All times are GMT +1. The time now is 06:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"