Excel VBA Method 'ClearContents' of Object Range Failed -


i've looked @ similar questions "method of object range failed" none seem me solve problem.

the error receive is: "method 'clearcontents' of object range failed"

essentially i'm trying achieve if cell value of bg21 = 1 , length of string in adjacent cell (bh21) 0 timestamp adjacent cell (bh21). ideally want repeat range (bg21:bg35). know use each method want error cleared first.

i don't want use cell formula because need timestamp moment cell bg21 changes 1. if use cell formula anytime workbook opened timestamp changes.

the code have is:

private sub worksheet_change(byval target range)    dim wb workbook, ws worksheet     set wb = thisworkbook    set ws = wb.worksheets("submittal kickoff meeting")      if (ws.range("$bg$21").value = 1 , len(ws.range("$bh$21").value) = 0 , target.address = ws.range("$bg$21"))         ws.range("$bh$21").value = format(now(), "m/dd/yyyy hh:mm:ss am/pm")     else         ws.range("$bh$21").clearcontents  '<--- error occurs here     end if end sub 

the error occurs on line:

ws.range("$bh$21").clearcontents 

i have tried:

ws.range("$bh$21").value = "" 

but received same error code

any advice??

.

i couldn't error, there 2 things in code made wrong.

  1. target.address = ws.range("$bg$21") default property range value, compare address value here.
  2. ws.range("$bh$21").clearcontents cause infinite loop, code changes worksheet, change event occurs , macro starts again. use enableevents avoid this.

    application.enableevents = false ws.range("$bh$21").clearcontents application.enableevents = true 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -