Solved

Excel VBA Copy the contents/formatting of a range to a closed workbook

Posted on 2014-07-23
3
612 Views
Last Modified: 2014-07-23
Hi

What Excel VBA code would I use to copy the an Excel range to another closed Excel spreadsheet
0
Comment
Question by:murbro
3 Comments
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 40214052
This will take the current selection, create a new workbook, and add the selection to Sheet1 Cell A1 of the new workbook and save it as copyRange.xlsm
Sub CopyRange()
    Dim xls As Excel.Worksheet
    Dim rng As Excel.Range
    Dim xlb As Excel.Workbook
    Set rng = Excel.Selection
    Set xlb = Workbooks.Add()
    xlb.Application.DisplayAlerts = False
    
    Set xls = xlb.Sheets("Sheet1")
    rng.Copy Destination:=xls.Range("A1")
    xlb.SaveAs "copyRange.xlsm", xlOpenXMLWorkbookMacroEnabled, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
    xlb.Close False
End Sub

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40215171
Thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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,…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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