a little vba help

mike760534211

New Member
Joined
Sep 25, 2014
Messages
13
i am using the following to clear all unlocked cells in multiple sheets of a workbook.

Code:
    Dim wks As Worksheet
 
    For Each wks In ThisWorkbook.Worksheets
        On Error Resume Next
        wks.UsedRange.Value = vbNullString
        Err.Clear: On Error GoTo -1: On Error GoTo 0
    Next wks
 
    Set wks = Nothing
it works great but for some reason it deletes all my hyperlinks in the workbook also. is there a way to correct this?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
i am using the following to clear all unlocked cells in multiple sheets of a workbook.

Code:
    Dim wks As Worksheet
 
    For Each wks In ThisWorkbook.Worksheets
        On Error Resume Next
        wks.UsedRange.Value = vbNullString
        Err.Clear: On Error GoTo -1: On Error GoTo 0
    Next wks
 
    Set wks = Nothing
it works great but for some reason it deletes all my hyperlinks in the workbook also. is there a way to correct this?

Are the cells containing your hyperlinks locked? If not, why not?
 
Upvote 0
See how this code works for you (it should be faster than the approach you are trying to use)...
Code:
Sub ClearUnlockedCells()
  Application.FindFormat.Clear
  Application.FindFormat.Locked = False
  Cells.Replace "*", "", xlWhole, SearchFormat:=True, ReplaceFormat:=False
  Application.FindFormat.Clear
End Sub
 
Upvote 0
Did Rick Rothstein's code work for you OP? I had a similar worksheet issue and tried it and it worked :) Yay. Thanks a bunch for the help.

I have not tested his but i did find some code that works but it is CRAZY slow...

Code:
Dim ws As Worksheet
For Each ws In Worksheets
With ws
For Each C In .UsedRange
If C.Locked = False Then C.ClearContents
Next C
End With
Next ws

I will be testing his also.
 
Upvote 0
This runs fast and keeps the hyperlinks intact but it doesnt clear any of the unlocked cells.

See how this code works for you (it should be faster than the approach you are trying to use)...
Code:
Sub ClearUnlockedCells()
  Application.FindFormat.Clear
  Application.FindFormat.Locked = False
  Cells.Replace "*", "", xlWhole, SearchFormat:=True, ReplaceFormat:=False
  Application.FindFormat.Clear
End Sub
 
Upvote 0
This runs fast and keeps the hyperlinks intact but it doesnt clear any of the unlocked cells.
The code I posted works fine for me (just tested it again). Are you sure the cells you think are unlocked are really unlocked? Just a reminder if you did something quick for testing purposes, if you copied any locked cells to the range you had unlocked, those cells would become locked again.
 
Upvote 0
Validated several times that the cells i want cleared are unlocked and it does not roll through the sheets to clear out the unlocked cells on all the sheets in the work book. I did find code that was fast and did check all worksheets int he book and clear out the unlocked cells. code is below.

Code:
Dim ws As Worksheet, WorkRange As Range, FoundCells As Range, Cell As Range
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> ActiveSheet.Name Then
        Set WorkRange = ws.UsedRange
        For Each Cell In WorkRange
            If Cell.Locked = False Then
                If FoundCells Is Nothing Then
                    Set FoundCells = Cell
                Else
                    Set FoundCells = Union(FoundCells, Cell)
                End If
            End If
        Next Cell
        On Error Resume Next
        If Not FoundCells Is Nothing Then
            FoundCells.ClearContents
            Set FoundCells = Nothing
        End If
        Set WorkRange = Nothing
    End If
Next ws


The code I posted works fine for me (just tested it again). Are you sure the cells you think are unlocked are really unlocked? Just a reminder if you did something quick for testing purposes, if you copied any locked cells to the range you had unlocked, those cells would become locked again.
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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