VBA Range Clear Code Causing Hangs in Shared Workbook

PWTNPB

New Member
Joined
Jul 21, 2011
Messages
3
I'm new to vba. I have a shared workbook with VBA macros. The first execution of the code works in a timely manner, however, subsequent executions takes minutes to complete (if it even completes). I have been removing code little by little to find the culprit. A snippet is below.

Code:
    daysAhead = InputBox("Input desired # of calendar days:")
    testDate = Date + daysAhead
      
    With Sheets(SheetToUse)
        .Activate
        .AutoFilterMode = False
    End With
    
    lrow = Application.Match("PROJECTS", Range("E:E"), 0) 
    If IsError(lrow) = True Then          
        MsgBox "Invalid Worksheet:  " & SheetToUse
        Exit Sub
    End If
      
   Set rng1 = RealUsedRange(SheetToUse)  'found this function by Googling 
   
   Range(Cells(1, 1), Cells(lrow - 3, rng1.Columns.Count)).Clear

The range clear line appears to cause the problems. If it is removed, my stripped down code works instantaneously. If added, the code hangs.
Also, as an added twist, if the workbook is unshared, the code works fine with and without the range clear.

Any clues would be greatly appreciated. I have not found anything particular about clearing cells with shared workbooks. I'm at a loss.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
if you step through line by line, does the vba throw and error

.clearcontents is also usable, try on a test copy
 
Upvote 0
Thanks mole999. Stepping through the code, when not in shared mode, does not throw any errors.
I tried .ClearContents and that eliminates the hang, but of course, I need to remove the formatting for subsequent runs.
As a workaround maybe I'll try deleting and inserting lines!!! :(
 
Upvote 0
try
a new line

Range(Cells(1, 1), Cells(lrow - 3, rng1.Columns.Count)).Clearformats
Range(Cells(1, 1), Cells(lrow - 3, rng1.Columns.Count)).Clearcontents

I've done it reversed as I'm not sure what is selecting

so the format will leave the values, then remove the contents
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,227
Members
449,303
Latest member
grantrob

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