Solved

Excel VBA

Posted on 2011-02-15
2
333 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
Comment Utility
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
Comment Utility
beautiful, thankyou
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now