Very Easy ActiveWorksheet question

lhernandez

Active Member
Joined
May 22, 2006
Messages
282
For some reason I am having problems checking if a sheet is active :rolleyes:
What I would like to do is as follows:
If Sheet1 = activeworksheet then
clear contents
end if

if Sheet2 is active then
clear (A1:D1)
end if

Can someone help me out on this simple task that I cannot get please :cry:
thank you
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
Try this.
Code:
Select Case ActiveSheet.Name
     Case "Sheet1"
          ActiveSheet.Cells.ClearContents
     Case "Sheet2"
          ActiveSheet.Range("A1:D1").ClearContents
End Select
 

patrickmuldoon99

Active Member
Joined
Jun 27, 2006
Messages
345
Hi lhernandez

You could use a select case statement on the activesheets name

Code:
Select Case Activesheet.name
case "Sheet1"
   'do your thing here
case "Sheet2"
  ' do something else here
case else
'   do stuff here that isnt satisfied by earlier conditions
end select

HTH

Patrick


Edit: Sorry, Norie got there first!!
 

JamieDuncan

Board Regular
Joined
Aug 23, 2006
Messages
132
try putting this code into sheet1 code

Code:
Private Sub Worksheet_Activate()
    Cells.ClearContents
End Sub

and this in sheet2 code

Code:
Private Sub Worksheet_Activate()
    Range("A1:D1").ClearContents
End Sub
 

lhernandez

Active Member
Joined
May 22, 2006
Messages
282

ADVERTISEMENT

the case for sheet1 will work, but not for sheet2.
 

Forum statistics

Threads
1,136,601
Messages
5,676,732
Members
419,648
Latest member
jmuench

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
Top