Copying Data multiple Worksheets in a workbook

BIGTONE559

Active Member
Joined
Apr 20, 2011
Messages
336
Hello,

I'm trying to understand the syntax for running the same functions on multiple worksheets in the workbook. I would like to check each page cell J6 for a date >07-1-11 if that condition is true i want to copy Cell "J6" and Range(Cells(cell.row,11,cell.row,13)) (from that respective worksheet) to Sheet1

I've noticed a lot of references to

Code:
Dim ws as worksheet

for each ws in activeworkbook.activeworksheet
'enter code here
however when it runs it only runs on the activesheet


Using Excel 2007 by the way
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
BIGTONE559,


If I understand you correctly.


Sample worksheets:
Excel Workbook
ABC
1Title ATitle BTitle C
2
3
4
Sheet1





Excel Workbook
JKL
67/9/2011K6 Sheet2L6 Sheet2
Sheet2





Excel Workbook
JKL
67/1/2011K6 Sheet3L6 Sheet3
Sheet3





Excel Workbook
JKL
67/19/2011K6 Sheet4L6 Sheet4
Sheet4





After the macro in worksheet Sheet1:


Excel Workbook
ABC
1Title ATitle BTitle C
27/9/2011K6 Sheet2L6 Sheet2
37/19/2011K6 Sheet4L6 Sheet4
4
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub Test()
Dim ws As Worksheet
Dim TestDate As Date, NR As Long
Application.ScreenUpdating = False

TestDate = "07-1-11"

For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Sheet1" Then
    If ws.Range("J6").Value > TestDate Then
      
      'What column are your copying to?
      'The next line of code is finding the next blank row in column A
      NR = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      
      'What is cell.Row?
      'ws.Range(Cells(cell.Row, 11), Cells(cell.Row, 13)).Copy Worksheets("Sheet1").Range("B" & NR)
      
      ws.Range("J6:L6").Copy Worksheets("Sheet1").Range("A" & NR)
      
    End If
  End If
Next ws

Worksheets("Sheet1").Activate
Application.ScreenUpdating = True
End Sub


Then run the Test macro.
 
Upvote 0
BIGTONE559,


If the a above is not correct, can we have a screenshots of one of the worksheets with the raw data, and what Sheet1 will look like after a macro?


You will generally get much more help (and faster) in this forum if you can post your small samples (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:
 
Upvote 0
Hello Hiker and thanks for your response.

I'm at home at the moment but generally speaking the code looks great. . .i'll give it a spin as soon as i get in tomorrow. Also when i made reference to the:

Range(Cells(cell.row etc. . . it was because i'm used to using for each cell in "Range" and so that's how i'd normally gather the data we want from a sheet. your line of code

'ws.Range("J6:L6").Copy Worksheets("Sheet1").Range("A" & NR)

appears to take care of what i was attempting. Conceptually i understand things however, i always have issues with the syntax. I'll check back in tomorrow but again thanks buddy
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

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