Select all the cells of a sheet via VBA

Sandie1712

New Member
Joined
Aug 10, 2017
Messages
33
Hello,

i am very new to VBA coding and trying to select all the cells in below mentioned code."cells.select"

But it's giving error"Run time error 1004" could you pl suggest the solution.

Thanks!!

Code:
Sub copyfromallopenworkbookstooneworkbook()
Dim wb As Workbook, ws As Worksheet, j As Long
j = 1


For Each wb In Workbooks
  wb.Activate
          'MsgBox wb.Name


For Each ws In Worksheets
 ws.Select
           'MsgBox ws.Name
Cells.Select
  Selection.Copy
  ThisWorkbook.Activate
  Sheets(j).Select
  Range("A1").Select
  ActiveCell.PasteSpecial xlPasteValues
  ws.Activate
  j = j + 1
  wb.Activate
  
  Next
  
  Next
  
 ThisWorkbook.Activate


End Sub
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Re: how to select all the cells of a sheet via VBA?

Do you have any hidden, merged cells, or protected cells?
 
Upvote 0
I'd guess it's the location of the code that is the problem, but you don't need to select or activate:

Code:
Sub copyfromallopenworkbookstooneworkbook()
Dim wb As Workbook, ws As Worksheet, j As Long
j = 1


For Each wb In Workbooks
          'MsgBox wb.Name


For Each ws In wb.Worksheets
           'MsgBox ws.Name
ws.Cells.Copy
  ThisWorkbook.Sheets(j).Range("A1").PasteSpecial xlPasteValues
  j = j + 1

  
  Next
  
  Next
  
 ThisWorkbook.Activate


End Sub
 
Upvote 0
Hello,

Joe 4 Nothing is hidden here!!

Rory A

Your suggested code for coping worked well, but it again showed the same error 1004 at Range("A1")

** All of yours suggestions and codes are more than welcome, whereas i am keen to know the issue with my coding for better understanding.


Thanks!!
 
Upvote 0
What is the exact error message please?
 
Upvote 0
You only answered one part of the three part question I asked.

Also, which module did you place your VBA code in?
 
Upvote 0
Hello,

pl help me with the right coding for concatenate of below cited, hope with that my issue will be resolved.

For i =1 to 10

cells("j"&i).value>Cells(i).value


Here "j" is column no.
 
Upvote 0
The format of Cells is Cell(row,column).
So, it would be Cells(i,"j")

However, Cells(i) is not valid, as it is missing the row reference.
 
Upvote 0

Forum statistics

Threads
1,215,727
Messages
6,126,519
Members
449,316
Latest member
sravya

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