Why does ThisWorkbook.Sheets.Range.Clearcontens give Application-defined or Object-defined error? [duplicate]









up vote
2
down vote

favorite













This question already has an answer here:



  • Excel VBA, getting range from an inactive sheet

    3 answers



I wonder why the following line gave




"Application-defined or Object-defined error"




If Sheet6 is selected, this line runs fine - no error message. However, if another sheet is selected, Excel throws the above error message.



ThisWorkbook.Sheets("Sheet6").Range(Cells(1, 2), Cells(12, 1000).End(xlToLeft)).ClearContents









share|improve this question















marked as duplicate by chris neilsen excel
Users with the  excel badge can single-handedly close excel questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 9 at 0:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    2
    down vote

    favorite













    This question already has an answer here:



    • Excel VBA, getting range from an inactive sheet

      3 answers



    I wonder why the following line gave




    "Application-defined or Object-defined error"




    If Sheet6 is selected, this line runs fine - no error message. However, if another sheet is selected, Excel throws the above error message.



    ThisWorkbook.Sheets("Sheet6").Range(Cells(1, 2), Cells(12, 1000).End(xlToLeft)).ClearContents









    share|improve this question















    marked as duplicate by chris neilsen excel
    Users with the  excel badge can single-handedly close excel questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 9 at 0:19


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite












      This question already has an answer here:



      • Excel VBA, getting range from an inactive sheet

        3 answers



      I wonder why the following line gave




      "Application-defined or Object-defined error"




      If Sheet6 is selected, this line runs fine - no error message. However, if another sheet is selected, Excel throws the above error message.



      ThisWorkbook.Sheets("Sheet6").Range(Cells(1, 2), Cells(12, 1000).End(xlToLeft)).ClearContents









      share|improve this question
















      This question already has an answer here:



      • Excel VBA, getting range from an inactive sheet

        3 answers



      I wonder why the following line gave




      "Application-defined or Object-defined error"




      If Sheet6 is selected, this line runs fine - no error message. However, if another sheet is selected, Excel throws the above error message.



      ThisWorkbook.Sheets("Sheet6").Range(Cells(1, 2), Cells(12, 1000).End(xlToLeft)).ClearContents




      This question already has an answer here:



      • Excel VBA, getting range from an inactive sheet

        3 answers







      excel vba excel-vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 23:32









      K.Dᴀᴠɪs

      5,976102140




      5,976102140










      asked Nov 8 at 23:18









      joehua

      123111




      123111




      marked as duplicate by chris neilsen excel
      Users with the  excel badge can single-handedly close excel questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 9 at 0:19


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by chris neilsen excel
      Users with the  excel badge can single-handedly close excel questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 9 at 0:19


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You need to qualify all your Range objects with your Worksheet.



          With ThisWorkbook.Worksheets("Sheet6")
          .Range(.Cells(1, 2), .Cells(12, 1000).End(xlToLeft)).ClearContents
          End With


          You didn't qualify Cells() with your worksheet, so it pulls from the ActiveSheet instead of Worksheets("Sheet6").



          So, remember that when your thinking "It only works when it's on that sheet" then this is going to be the issue.






          share|improve this answer






















          • Thanks. Now that you pointed it out, I remember I had a similar problem few years back. I had solved the problem by qualifying Cells but the solution escaped me this time.
            – joehua
            Nov 9 at 0:25

















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          You need to qualify all your Range objects with your Worksheet.



          With ThisWorkbook.Worksheets("Sheet6")
          .Range(.Cells(1, 2), .Cells(12, 1000).End(xlToLeft)).ClearContents
          End With


          You didn't qualify Cells() with your worksheet, so it pulls from the ActiveSheet instead of Worksheets("Sheet6").



          So, remember that when your thinking "It only works when it's on that sheet" then this is going to be the issue.






          share|improve this answer






















          • Thanks. Now that you pointed it out, I remember I had a similar problem few years back. I had solved the problem by qualifying Cells but the solution escaped me this time.
            – joehua
            Nov 9 at 0:25














          up vote
          4
          down vote



          accepted










          You need to qualify all your Range objects with your Worksheet.



          With ThisWorkbook.Worksheets("Sheet6")
          .Range(.Cells(1, 2), .Cells(12, 1000).End(xlToLeft)).ClearContents
          End With


          You didn't qualify Cells() with your worksheet, so it pulls from the ActiveSheet instead of Worksheets("Sheet6").



          So, remember that when your thinking "It only works when it's on that sheet" then this is going to be the issue.






          share|improve this answer






















          • Thanks. Now that you pointed it out, I remember I had a similar problem few years back. I had solved the problem by qualifying Cells but the solution escaped me this time.
            – joehua
            Nov 9 at 0:25












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          You need to qualify all your Range objects with your Worksheet.



          With ThisWorkbook.Worksheets("Sheet6")
          .Range(.Cells(1, 2), .Cells(12, 1000).End(xlToLeft)).ClearContents
          End With


          You didn't qualify Cells() with your worksheet, so it pulls from the ActiveSheet instead of Worksheets("Sheet6").



          So, remember that when your thinking "It only works when it's on that sheet" then this is going to be the issue.






          share|improve this answer














          You need to qualify all your Range objects with your Worksheet.



          With ThisWorkbook.Worksheets("Sheet6")
          .Range(.Cells(1, 2), .Cells(12, 1000).End(xlToLeft)).ClearContents
          End With


          You didn't qualify Cells() with your worksheet, so it pulls from the ActiveSheet instead of Worksheets("Sheet6").



          So, remember that when your thinking "It only works when it's on that sheet" then this is going to be the issue.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 23:25

























          answered Nov 8 at 23:20









          K.Dᴀᴠɪs

          5,976102140




          5,976102140











          • Thanks. Now that you pointed it out, I remember I had a similar problem few years back. I had solved the problem by qualifying Cells but the solution escaped me this time.
            – joehua
            Nov 9 at 0:25
















          • Thanks. Now that you pointed it out, I remember I had a similar problem few years back. I had solved the problem by qualifying Cells but the solution escaped me this time.
            – joehua
            Nov 9 at 0:25















          Thanks. Now that you pointed it out, I remember I had a similar problem few years back. I had solved the problem by qualifying Cells but the solution escaped me this time.
          – joehua
          Nov 9 at 0:25




          Thanks. Now that you pointed it out, I remember I had a similar problem few years back. I had solved the problem by qualifying Cells but the solution escaped me this time.
          – joehua
          Nov 9 at 0:25



          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)