You are here:

Excel/Copy from data from one workbook to another

Advertisement


Question
Hello Bob,

I need you assistance, I have a macro that will copy data from one sheet to another if it qualify the criteria. NOw, I want the macro to copy it to another workbook instead of just another worksheet, can you assist me with this please? Below is the macro:

Sub REPORTING()
Application.ScreenUpdating = False
Application.StatusBar = "Job Register Daily Report is currently updating, please wait."


'COPY ALL REPORT ITEMS TO "REPORT" SHEET
Sheets("REPORT").Select
With Sheets("REPORT")
   .UsedRange.Offset(6).ClearContents
   .UsedRange.Offset(6).ClearFormats
End With
Dim lr As Long, lr2 As Long, r As Long, ws1 As Worksheet, ws2 As Worksheet, wb1 As Workbook, wb2 As Workbook, N As Long
Set ws1 = Sheets("ATWS")
Set ws2 = Sheets("REPORT")


N = 6
Try = Cells(1, 1)
lr = ws1.Cells(Rows.Count, "Z").End(xlUp).Row
lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row


   For r = 6 To lr
       If ws1.Range("Z" & r).Value = "Try" Or ws1.Range("Z" & r).Value = "YES" Or ws1.Range("Z" & r).Value = "PO" Then
       
         ws1.Cells(r, 7).Copy
         ws2.Cells(N, 1).Select
         Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         
         ws1.Cells(r, 11).Copy
         ws2.Cells(N, 2).Select
         Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         
         ws1.Cells(r, 19).Copy
         ws2.Cells(N, 3).Select
         Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         
         
         N = N + 1
       End If
   Next r
Sheets("REPORT").Select
Application.StatusBar = False
End Sub

Answer
Sub REPORTING()
   Application.ScreenUpdating = False
   Application.StatusBar = "Job Register Daily Report is currently updating, please wait."


   'COPY ALL REPORT ITEMS TO "REPORT" SHEET
   Workbooks.Add
   Sheets(1).Name = "REPORT"
   
   Dim lr As Long, r As Long, ws1 As Worksheet, ws2 As Worksheet, wb1 As Workbook, wb2 As Workbook, N As Long
   Set ws1 = ThisWorkbook.Sheets("ATWS")
   Set ws2 = Sheets("REPORT")

   ThisWorkbook.Activate
   N = 6
   Try = Cells(1, 1)
   lr = ws1.Cells(Rows.Count, "Z").End(xlUp).Row

   For r = 6 To lr
       If ws1.Range("Z" & r).Value = "Try" Or ws1.Range("Z" & r).Value = "YES" Or ws1.Range("Z" & r).Value = "PO" Then

         ws1.Cells(r, 7).Copy
         ws2.Cells(N, 1).PasteSpecial

         ws1.Cells(r, 11).Copy
         ws2.Cells(N, 2).PasteSpecial

         ws1.Cells(r, 19).Copy
         ws2.Cells(N, 3).PasteSpecial

         N = N + 1
       End If
   Next r
   ws2.Parent.Activate
   Sheets("REPORT").Select
   Application.StatusBar = False
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.