PC Review


Reply
Thread Tools Rate Thread

Clearing unlocked cell contents macro

 
 
Steve
Guest
Posts: n/a
 
      1st Feb 2011
HYCH

Would like to run a macro with the following for several pages, but
only want to clear contents of unlocked cell within the worksheet,

rather than scrolling through and doing it manually?

any help with this would be wonderful

Steve


 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      1st Feb 2011
Try this one. Note that the sheet has to be unprotected to use the
SpecialCells function.

Sub ClearUnlockedCells()
Dim sh As Worksheet
Dim cell As Range
Dim pWord As String

For Each sh In ThisWorkbook.Sheets
If sh.Name <> "NotThisSheet" Then
sh.Unprotect Password:=pWord
For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
If cell.Locked = False Then
cell.Clear
End If
Next
sh.Protect Password:=pWord
End If
Next
End Sub

Regards,
Per

On 1 Feb., 17:16, Steve <instruct...@hotmail.com> wrote:
> HYCH
>
> Would like to run a macro with the following for several pages, but
> only want to clear contents of unlocked cell within the worksheet,
>
> rather than scrolling through and doing it manually?
>
> any help with this would be wonderful
>
> Steve


 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      2nd Feb 2011
On Feb 1, 6:05*pm, Per Jessen <perjesse...@hotmail.com> wrote:
> Try this one. Note that the sheet has to be unprotected to use the
> SpecialCells function.
>
> Sub ClearUnlockedCells()
> Dim sh As Worksheet
> Dim cell As Range
> Dim pWord As String
>
> For Each sh In ThisWorkbook.Sheets
> * * If sh.Name <> "NotThisSheet" Then
> * * * * sh.Unprotect Password:=pWord
> * * * * For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
> * * * * * * If cell.Locked = False Then
> * * * * * * * * cell.Clear
> * * * * * * End If
> * * * * Next
> * * * * sh.Protect Password:=pWord
> * * End If
> Next
> End Sub
>
> Regards,
> Per




Per, thank you very much for your assistance, the sheets are
protected, but without password, so can cover this aspect with simply
unprotecting the sheet and re applying after this macro has run, have
22 worksheets, but only need to clear data in the first 7,

as i read your code please excuse me if i am wrong but this would
unprotect every sheet?

would i need to adjust the code to just unprotect Sheet1:Sheet7

Just some clarification

Again many thanks

Steve
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      3rd Feb 2011
On Feb 2, 8:35*am, Steve <instruct...@hotmail.com> wrote:
> On Feb 1, 6:05*pm, Per Jessen <perjesse...@hotmail.com> wrote:
>
>
>
>
>
> > Try this one. Note that the sheet has to be unprotected to use the
> > SpecialCells function.

>
> > Sub ClearUnlockedCells()
> > Dim sh As Worksheet
> > Dim cell As Range
> > Dim pWord As String

>
> > For Each sh In ThisWorkbook.Sheets
> > * * If sh.Name <> "NotThisSheet" Then
> > * * * * sh.Unprotect Password:=pWord
> > * * * * For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
> > * * * * * * If cell.Locked = False Then
> > * * * * * * * * cell.Clear
> > * * * * * * End If
> > * * * * Next
> > * * * * sh.Protect Password:=pWord
> > * * End If
> > Next
> > End Sub

>
> > Regards,
> > Per

>
> Per, thank you very much for your assistance, the sheets are
> protected, but without password, so can cover this aspect with simply
> unprotecting the sheet and re applying after this macro has run, have
> 22 worksheets, but only need to clear data in the first 7,
>
> as i read your code please excuse me if i am wrong but this would
> unprotect every sheet?
>
> would i need to adjust the code to just unprotect Sheet1:Sheet7
>
> Just some clarification
>
> Again many thanks
>
> Steve- Hide quoted text -
>
> - Show quoted text




have used the code below that was provided for me by pers, but am
getting an error message

Run time error '1004'
No cells were found


There are actually unlocked cells in the spreadsheets with information
in them that was expecting to be cleared

any help with this please
am using office 2003

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("w96").Value <> 0 Then
Dim sh As Worksheet
Dim cell As Range
For Each sh In ThisWorkbook.Sheets
If sh.Name <> "wages sheet" Then
sh.Unprotect
For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
If cell.Locked = False Then
cell.Clear
End If
Next
sh.Protect
End If
Next
End If
End Sub


Steve
 
Reply With Quote
 
Per Jessen
Guest
Posts: n/a
 
      3rd Feb 2011
There has to be a sheet with no information to delete. I have added an
error check.

As you asked in your previous post, I have changed the loop, so the
macro loop through Sheet1:Sheet7

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("w96").Value <> 0 Then
Dim sh As Worksheet
Dim cell As Range
Dim c

For s = 1 To 7
Set sh = Worksheets("Sheet" & s)
sh.Unprotect
On Error Resume Next
c = sh.Cells.SpecialCells(xlCellTypeConstants).Count
If Err.Number = 0 Then
For Each cell In
sh.Cells.SpecialCells(xlCellTypeConstants)

If cell.Locked = False Then
cell.Clear
End If
Next
sh.Protect
End If
On Error GoTo 0
Next
End If
End Sub

Regards,
Per

On 3 Feb., 13:33, Steve <instruct...@hotmail.com> wrote:
> On Feb 2, 8:35*am, Steve <instruct...@hotmail.com> wrote:
>
>
>
>
>
> > On Feb 1, 6:05*pm, Per Jessen <perjesse...@hotmail.com> wrote:

>
> > > Try this one. Note that the sheet has to be unprotected to use the
> > > SpecialCells function.

>
> > > Sub ClearUnlockedCells()
> > > Dim sh As Worksheet
> > > Dim cell As Range
> > > Dim pWord As String

>
> > > For Each sh In ThisWorkbook.Sheets
> > > * * If sh.Name <> "NotThisSheet" Then
> > > * * * * sh.Unprotect Password:=pWord
> > > * * * * For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
> > > * * * * * * If cell.Locked = False Then
> > > * * * * * * * * cell.Clear
> > > * * * * * * End If
> > > * * * * Next
> > > * * * * sh.Protect Password:=pWord
> > > * * End If
> > > Next
> > > End Sub

>
> > > Regards,
> > > Per

>
> > Per, thank you very much for your assistance, the sheets are
> > protected, but without password, so can cover this aspect with simply
> > unprotecting the sheet and re applying after this macro has run, have
> > 22 worksheets, but only need to clear data in the first 7,

>
> > as i read your code please excuse me if i am wrong but this would
> > unprotect every sheet?

>
> > would i need to adjust the code to just unprotect Sheet1:Sheet7

>
> > Just some clarification

>
> > Again many thanks

>
> > Steve- Hide quoted text -

>
> > - Show quoted text

>
> have used the code below that was provided for me by pers, but am
> getting an error message
>
> Run time error '1004'
> No cells were found
>
> There are actually unlocked cells in the spreadsheets with information
> in them that was expecting to be cleared
>
> any help with this please
> am using office 2003
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Range("w96").Value <> 0 Then
> * * Dim sh As Worksheet
> * * Dim cell As Range
> * * For Each sh In ThisWorkbook.Sheets
> * * If sh.Name <> "wages sheet" Then
> * * * * sh.Unprotect
> * * * * For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
> * * * * * * If cell.Locked = False Then
> * * * * * * * * cell.Clear
> * * * * * * End If
> * * Next
> * * * * sh.Protect
> * * End If
> Next
> End If
> End Sub
>
> Steve- Skjul tekst i anførselstegn -
>
> - Vis tekst i anførselstegn -


 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      3rd Feb 2011
On Feb 3, 2:23*pm, Per Jessen <perjesse...@hotmail.com> wrote:
> There has to be a sheet with no information to delete. I have added an
> error check.
>
> As you asked in your previous post, I have changed the loop, so the
> macro loop through Sheet1:Sheet7
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Range("w96").Value <> 0 Then
> * * Dim sh As Worksheet
> * * Dim cell As Range
> * * Dim c
>
> * * For s = 1 To 7
> * * * * Set sh = Worksheets("Sheet" & s)
> * * * * sh.Unprotect
> * * * * On Error Resume Next
> * * * * c = sh.Cells.SpecialCells(xlCellTypeConstants).Count
> * * * * If Err.Number = 0 Then
> * * * * * * For Each cell In
> sh.Cells.SpecialCells(xlCellTypeConstants)
>
> * * * * * * * * If cell.Locked = False Then
> * * * * * * * * * * cell.Clear
> * * * * * * * * End If
> * * * * * * Next
> * * * * * * sh.Protect
> * * * * End If
> * * * * On Error GoTo 0
> * * Next
> End If
> End Sub
>
> Regards,
> Per
>
> On 3 Feb., 13:33, Steve <instruct...@hotmail.com> wrote:
>
>
>
> > On Feb 2, 8:35*am, Steve <instruct...@hotmail.com> wrote:

>
> > > On Feb 1, 6:05*pm, Per Jessen <perjesse...@hotmail.com> wrote:

>
> > > > Try this one. Note that the sheet has to be unprotected to use the
> > > > SpecialCells function.

>
> > > > Sub ClearUnlockedCells()
> > > > Dim sh As Worksheet
> > > > Dim cell As Range
> > > > Dim pWord As String

>
> > > > For Each sh In ThisWorkbook.Sheets
> > > > * * If sh.Name <> "NotThisSheet" Then
> > > > * * * * sh.Unprotect Password:=pWord
> > > > * * * * For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
> > > > * * * * * * If cell.Locked = False Then
> > > > * * * * * * * * cell.Clear
> > > > * * * * * * End If
> > > > * * * * Next
> > > > * * * * sh.Protect Password:=pWord
> > > > * * End If
> > > > Next
> > > > End Sub

>
> > > > Regards,
> > > > Per

>
> > > Per, thank you very much for your assistance, the sheets are
> > > protected, but without password, so can cover this aspect with simply
> > > unprotecting the sheet and re applying after this macro has run, have
> > > 22 worksheets, but only need to clear data in the first 7,

>
> > > as i read your code please excuse me if i am wrong but this would
> > > unprotect every sheet?

>
> > > would i need to adjust the code to just unprotect Sheet1:Sheet7

>
> > > Just some clarification

>
> > > Again many thanks

>
> > > Steve- Hide quoted text -

>
> > > - Show quoted text

>
> > have used the code below that was provided for me by pers, but am
> > getting an error message

>
> > Run time error '1004'
> > No cells were found

>
> > There are actually unlocked cells in the spreadsheets with information
> > in them that was expecting to be cleared

>
> > any help with this please
> > am using office 2003

>
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > If Range("w96").Value <> 0 Then
> > * * Dim sh As Worksheet
> > * * Dim cell As Range
> > * * For Each sh In ThisWorkbook.Sheets
> > * * If sh.Name <> "wages sheet" Then
> > * * * * sh.Unprotect
> > * * * * For Each cell In sh.Cells.SpecialCells(xlCellTypeConstants)
> > * * * * * * If cell.Locked = False Then
> > * * * * * * * * cell.Clear
> > * * * * * * End If
> > * * Next
> > * * * * sh.Protect
> > * * End If
> > Next
> > End If
> > End Sub

>
> > Steve- Skjul tekst i anførselstegn -

>
> > - Vis tekst i anførselstegn -- Hide quoted text -

>
> - Show quoted text -


Per,

Your an absolute star, many thanks for your assistance

works perfectly

Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for clearing cell contents Sal Microsoft Excel Misc 5 9th Jan 2009 11:40 PM
Macro: Clear contents of unlocked cells =?Utf-8?B?VHVja2xhbmQ=?= Microsoft Excel Programming 5 14th Jun 2007 12:13 PM
Macro Help for Deleting Blank Rows & Clearing Cell Contents ksp Microsoft Excel Programming 2 13th Jan 2006 05:25 AM
Clearing Contents of Cell Burt Microsoft Excel Worksheet Functions 1 4th May 2005 02:46 PM
hide contents of an unlocked cell Tom Microsoft Excel Programming 0 25th Sep 2003 08:39 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:10 PM.