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
Solved

Excel VBA

Posted on 2011-02-15
2
337 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
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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 …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

789 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