Clear Range "Clear Method of Range Failed"

talkinggoat

New Member
Joined
Feb 1, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
My Code:
VBA Code:
ClearFormImportSheetsButton "Sheet1"

Sub ClearFormImportSheetsButton(Fsheet As String)
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets(Fsheet)
   
    sh.Range("A1").Value = "A"
    lrowDel = sh.Cells.Find(what:="*", _
            after:=Range("a1"), _
            lookat:=xlPart, _
            LookIn:=xlFormulas, _
            searchorder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row
           
    lcolDel = sh.Cells.Find(what:="*", _
            after:=Range("A1"), _
            lookat:=xlPart, _
            LookIn:=xlFormulas, _
            searchorder:=xlByColumns, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Column
           
            Debug.Print "Clearing sheet. " & Fsheet
           
    sh.Range(Cells(8, 1), Cells(lrowDel, lcolDel)).Clear
   
End Sub

I have to insert something into at least one of the cells, to ensure the Find methods work... they have to have something to find, or they produce an error, so I insert a single character into the first cell of the data portion of the sheet. That way, it will always have something to clear. This works fine if I manually type A or whatever, but if I let VBA enter the value, it produces the error, "Clear Method of Range Failed", even though I know it's putting A in the cell, as the value. It doesn't matter if I change it to Range(A8).Clear, ActiveSheet.Range(A8).Clear it produces the error, but if I manually type something in there, it works fine. Other methods, such as Select or Value work fine. Only Clear will produce an error.

I also checked the Locals window to ensure lrowDel and lcolDel are receiving numbers, which they are.
 

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.
It seems this is caused because I'm not ACTIVELY in the sheet I'm trying to modify, but I though that was the purpose of explicitly defining the sheet in,
set sh = ThisWorkbook.Sheets("Sheet1")
and then using sh to identify every command after that.
sh.cells.find
sh.range
... etc
 
Upvote 0
You need to specify the sheet for the cells, not just the range
VBA Code:
sh.Range(sh.Cells(8, 1), sh.Cells(lrowDel, lcolDel)).Clear
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,046
Members
449,063
Latest member
ak94

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