Solved

Excel VBA

Posted on 2011-02-15
2
339 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

705 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