Solved

Copy a Unidifiend Range, Starting at A11

Posted on 2011-02-16
6
379 Views
Last Modified: 2012-08-13
Good Morning

I am looking to have a macro which will copy all the rows from A11 to the next blank row, allowing for different amounts of data. For example I could have the following ranges:

A11 - A21
A11 - A33
A11 - A42

But will always have a blank row at the end of the range I wish to copy.

Many thanks
0
Comment
Question by:Washcare
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:andreizz
ID: 34904613
A11 - A21
A11 - A33
A11 - A42
Do you wish to copy them from A11 to B11 or append them after A21 like A22-A32?
0
 

Author Comment

by:Washcare
ID: 34904758
Andreizz

I will be looking to append them to the end of an existing set of rows in another workbook?

So from Workbook1, Sheet10
To Workbook2, Sheet5, add to last blank row, looking in column A

Many thanks
0
 
LVL 12

Expert Comment

by:sstampf
ID: 34905079
Try this code, make changes in the variables as needed or ask me if you have any doubts.

Also, see:
http://www.ozgrid.com/forum/showthread.php?t=26768&page=1
Sub AppendData()

    Dim wbkMaster As Workbook
    Dim shtMaster As Worksheet
    Dim rngMaster As Range
    Dim wbkData As Workbook
    Dim shtData As Worksheet
    Dim rngData As Range
    
' change path and file name to suit
    Set wbkMaster = Workbooks.Open("D:\temp\testmaster.xls")
    Set shtMaster = wbkMaster.Worksheets(1)
    
' change path and file name to suit
    Set wbkData = Workbooks.Open("D:\temp\test1.xls")
    Set shtData = wbkData.Worksheets(1)
    
' get end of master
    Set rngMaster = shtMaster.Range("A65536").End(xlUp)
' Change as per your need
    Set rngData = shtData.Range("A11:A31")
' copy data across
    rngData.Copy rngMaster
    
    MsgBox "Appended " & rngData.Rows.Count & " rows of data to Master data", vbInformation
    
' simply close data
    wbkData.Close False
' save and close master
    wbkMaster.Close True

'  release objects
    Set rngData = Nothing
    Set shtData = Nothing
    Set wbkData = Nothing
    Set rngMaster = Nothing
    Set shtMaster = Nothing
    Set wbkMaster = Nothing
End Sub

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 12

Accepted Solution

by:
sstampf earned 500 total points
ID: 34905130
If you want to copy - paste all the data from A11 to the last row then use this code
Sub AppendData()

    Dim wbkMaster As Workbook
    Dim shtMaster As Worksheet
    Dim rngMaster As Range
    Dim wbkData As Workbook
    Dim shtData As Worksheet
    Dim rngData As Range
    
' change path and file name to suit
    Set wbkMaster = Workbooks.Open("D:\temp\testmaster.xls")
    Set shtMaster = wbkMaster.Worksheets(1)
    
' change path and file name to suit
    Set wbkData = Workbooks.Open("D:\temp\test1.xls")
    Set shtData = wbkData.Worksheets(1)
    
' get end of master
    Set rngMaster = shtMaster.Range("A65536").End(xlUp)
' Change as per your need
    Set rngData = shtData.Range("A11:A" & shtData.Range("A11").End(xlDown).Row)
' copy data across
    rngData.Copy rngMaster
    
    MsgBox "Appended " & rngData.Rows.Count & " rows of data to Master data", vbInformation
    
' simply close data
    wbkData.Close False
' save and close master
    wbkMaster.Close True

'  release objects
    Set rngData = Nothing
    Set shtData = Nothing
    Set wbkData = Nothing
    Set rngMaster = Nothing
    Set shtMaster = Nothing
    Set wbkMaster = Nothing
End Sub

Open in new window

0
 

Author Comment

by:Washcare
ID: 34905674
stampf

Is there a way to change this line, so that it works with any file named in the directory?

Set wbkData = Workbooks.Open("D:\temp\test1.xls")

Something along the lines of:

Set wbkData = Workbooks.Open("D:\temp\*.xls")

Many thanks
0
 

Author Closing Comment

by:Washcare
ID: 34905961
Many thanks
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

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 article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

820 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