VBA code to select all the data on a worksheet

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

Please help me out with a VBA code to select all the data on a worksheet.
I am using the following code:

Sheets(1).Select
Range("A1").Select
On Error Resume Next
mylastrow = Cells.Find("*", [a1], , , xlByRows, xlPrevious).Row
mylastcol = Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
mylastcell = Cells(mylastrow, mylastcol).Address
myrange = "A2:" & mylastcell
Range(myrange).Select
Selection.Copy

I am not getting what is the problem with this code. It works sometimes and sometimes not.

Please help me out on this.

Thanks!

Regards,
Shweta
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Code:
Sheets(1).Select
Range("A1").Select
On Error Resume Next
Set mylastcell = Cells(1, 1).SpecialCells(xlLastCell)
mylastcelladd = Cells(mylastcell.Row, mylastcell.Column).Address
myrange = "A2:" & mylastcelladd
Range(myrange).Select
Selection.Copy

use this instead.
Your codes do work for me though.
 
Upvote 0
Thanks Prajul for your quick response!

your code is doing the same as my code is doing. It is selecting only range("A1")
 
Upvote 0
Then the only possibility is that you entire sheet is either completely blank or only first row has some values.
 
Last edited:
Upvote 0
Why not....
Code:
Sub alldat()
ActiveSheet.UsedRange.Copy
End Sub
 
Upvote 0
Thanks Michael!
Your code is working fine.

would like to tell you that my code and prajul's code is working good on another file.

Regards,
Shweta
 
Upvote 0
what about using this

Cells.Select

That seems to work for me. You would just need to specify which sheet.
 
Upvote 0
Thanks Michael, works perfectly.
Prajul, no also only selected one cell with my data.
jacfox68, no that selects the whole sheet and only works if you are pasting the whole sheet into A1 on another sheet.

I have multiple files (each only one sheet and about 700 lines), I want to copy all [active] cells to one new workbook. so I used a dir loop to open each file sequentially, Michael's used range,
ActiveCell.SpecialCells(xlLastCell).Offset(2, 0).End(xlToLeft).Select to paste into my combined file
(and a few other lines of coding before someone says that didn't work for them... :) )
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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