Solved

Excel VBA

Posted on 2011-02-15
2
338 Views
Last Modified: 2012-05-11
how do i change this vba code below to copy the data to a sheet named results rather than opening a workbook and copying the results to the sheet in the workbook.

Sub CopyData()
 
Dim ReportWB As Workbook
Dim DestinationWS As Worksheet
Dim LastRow As Range
 
Application.Workbooks.Open Filename:="M:\K REPORTS REDOING\K_Report_Test.xlsm"
 
Set ReportWB = Application.Workbooks(Application.Workbooks.Count)
Set DestinationWS = ReportWB.Worksheets("Data")
 
For Each coy In Array("1", "2", "A", "C")
    With Sheet6
        Set LastRow = DestinationWS.Range("A1048576").End(xlUp).Offset(1)
        .Range("F1").Value = coy
        Sheet9.Cells.Delete
        .Range("A4:Q1000").AdvancedFilter xlFilterCopy, .Range("Q1:Q2"), Sheet9.Range("A1")
        Intersect(Sheet9.UsedRange, Sheet9.Range("2:1048576")).Copy LastRow
 
    End With
Next coy
 
Set LastRow = Nothing
Set DestinationWS = Nothing
Set ReportWB = Nothing
 
End Sub
 
0
Comment
Question by:JKCC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34895764
Like so:
Sub CopyData()
 
Dim DestinationWS As Worksheet
Dim LastRow As Range
 
Set DestinationWS = Sheets("Results")
 
For Each coy In Array("1", "2", "A", "C")
    With Sheet6
        Set LastRow = DestinationWS.Cells(Rows.Count, "A").End(xlUp).Offset(1)
        .Range("F1").Value = coy
        Sheet9.Cells.Delete
        .Range("A4:Q1000").AdvancedFilter xlFilterCopy, .Range("Q1:Q2"), Sheet9.Range("A1")
        Intersect(Sheet9.UsedRange, Sheet9.Range("2:" & .rows.Count)).Copy LastRow
 
    End With
Next coy
 
Set LastRow = Nothing
Set DestinationWS = Nothing
 
End Sub

Open in new window

0
 

Author Closing Comment

by:JKCC
ID: 34895857
beautiful, thankyou
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question