Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Select and Activate Cells — Select

Select:

You can Select a cells or cells for per­form­ing many activ­i­ties like putting some value in cell or doing for­mat­ting or copy-paste the data. You can put or enter val­ues in cells with/without select­ing the cells. Select is used mostly in the cases of copy-paste oper­a­tion where you have to tell the com­piler specif­i­cally that from which cell it has to copy the data and in which cell it needs to be pasted.

Note:

Select works only with actives work­sheets, if you use Select on cells before acti­vat­ing the respec­tive sheet, the select method will fail and you will get “Run-time error ‘1004’: Appli­ca­tion defined or object defined error”

Example:

Sub CopyPasteData()

   Worksheets("Sheet1").Range("A1").Copy

   Worksheets("Sheet3").Range("A1").Select

   Worksheets("Sheet3").Paste

End Sub

If you run this pro­ce­dure, you will, get “Run-time error ‘1004’: Appli­ca­tion defined or object defined error”

All you need to do is, just acti­vate the sheet3 before select­ing the cell from it and past­ing the data

Sub CopyPasteData()

    Worksheets("Sheet1").Range("A1").Copy

 Worksheets("Sheet3").Select ' Activate the sheet3

   Worksheets("Sheet3").Range("A1").Select

   Worksheets("Sheet3").Paste

End Sub 

Now this code will run fine and paste the A1 cell data from sheet1 to sheet3.

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: