Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default Working with a worksheet that is not the active worksheet

How can I make this code work on my worksheet named February, when it is not
the active worksheet?


Sub BlankWeeks()
'
' Macro2 Macro
'

'
If Range("C184").Value = "" Then
Rows("184:228").Hidden = True
End If

If Range("C184").Value < "" Then
Rows("184:228").Hidden = False
End If

If Range("C229").Value = "" Then
Rows("229:273").Hidden = True
End If

If Range("C229").Value < "" Then
Rows("229:273").Hidden = False
End If

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Working with a worksheet that is not the active worksheet

Hi,

You have to qualify the ranges with a sheet name

Sub BlankWeeks()
Set sht = Sheets("February")
With sht
If .Range("C184").Value = "" Then
.Rows("184:228").Hidden = True
End If

If .Range("C229").Value < "" Then
.Rows("229:273").Hidden = False
End If
End With
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"ordnance1" wrote:

How can I make this code work on my worksheet named February, when it is not
the active worksheet?


Sub BlankWeeks()
'
' Macro2 Macro
'

'
If Range("C184").Value = "" Then
Rows("184:228").Hidden = True
End If

If Range("C184").Value < "" Then
Rows("184:228").Hidden = False
End If

If Range("C229").Value = "" Then
Rows("229:273").Hidden = True
End If

If Range("C229").Value < "" Then
Rows("229:273").Hidden = False
End If

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Working with a worksheet that is not the active worksheet

You have to tell the VB processor the name of your worksheet which you can
do using the Worksheets collection. There are two ways to do this. First, by
directly qualifying each range reference...

Sub BlankWeeks()
If Worksheets("February").Range("C184").Value = "" Then
Worksheets("February").Rows("184:228").Hidden = True
End If
If Worksheets("February").Range("C184").Value < "" Then
Worksheets("February").Rows("184:228").Hidden = False
End If
If Worksheets("February").Range("C229").Value = "" Then
Worksheets("February").Rows("229:273").Hidden = True
End If
If Worksheets("February").Range("C229").Value < "" Then
Worksheets("February").Rows("229:273").Hidden = False
End If
End Sub

or second (the much cleaner looking way), by using a With/End With block...

Sub BlankWeeks()
With Worksheets("February")
If .Range("C184").Value = "" Then
.Rows("184:228").Hidden = True
End If
If .Range("C184").Value < "" Then
.Rows("184:228").Hidden = False
End If
If .Range("C229").Value = "" Then
.Rows("229:273").Hidden = True
End If
If .Range("C229").Value < "" Then
.Rows("229:273").Hidden = False
End If
End With
End Sub

Notice the "dots" in front of each range reference (Range, rows, etc.)...
those are required in order that the range references back to the object of
the With statement.

--
Rick (MVP - Excel)



"ordnance1" wrote in message
...
How can I make this code work on my worksheet named February, when it is
not the active worksheet?


Sub BlankWeeks()
'
' Macro2 Macro
'

'
If Range("C184").Value = "" Then
Rows("184:228").Hidden = True
End If

If Range("C184").Value < "" Then
Rows("184:228").Hidden = False
End If

If Range("C229").Value = "" Then
Rows("229:273").Hidden = True
End If

If Range("C229").Value < "" Then
Rows("229:273").Hidden = False
End If

End Sub


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
Copying the active worksheet to another worksheet in the same work queenmaam Excel Worksheet Functions 5 March 25th 08 06:56 PM
Deleting column in a worksheet that is not active worksheet [email protected] Excel Programming 3 October 7th 07 03:33 PM
Basic Question - How do I return the worksheet number of the active worksheet? Regnab Excel Programming 2 May 17th 06 03:02 AM
Altering code to reference the worksheet before the active worksheet KimberlyC Excel Programming 8 March 15th 05 10:26 PM
macro to apply worksheet event to active worksheet Paul Simon[_2_] Excel Programming 3 August 7th 03 02:50 AM


All times are GMT +1. The time now is 07:17 AM.

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

About Us

"It's about Microsoft Excel"