Be the first user to complete this post

  • 0
Add to List

VBA-Excel: Select and Activate Cells - Select

Select:

You can Select a cells or cells for performing many activities like putting some value in cell or doing formatting or copy-paste the data. You can put or enter values in cells with/without selecting the cells. Select is used mostly in the cases of copy-paste operation where you have to tell the compiler specifically 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 worksheets, if you use Select on cells before activating the respective sheet, the select method will fail and you will get “Run-time error ‘1004’: Application 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 procedure, you will, get “Run-time error ‘1004’: Application defined or object defined error”

All you need to do is, just activate the sheet3 before selecting the cell from it and pasting 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.



Also Read:

  1. VBA-Excel: Date-Time Functions – IsDate()
  2. VBA-Excel: String Functions – String()
  3. VBA-Excel: Add Worksheets For All The Given Dates Except Weekends and Copy The Common Template In Each Worksheet
  4. VBA Excel - Cells, Ranges and Offset : Offset
  5. VBA Codes - Record Macro