Solved

Save Specified Contents in Spreadsheet Into New Spreadsheet

Posted on 2011-02-19
6
821 Views
Last Modified: 2012-05-11
I want to copy the specified range of cells data into another new spreadsheet at a click of commandbutton

Ex: If have B5:G20 the range where i need the data from the existing spreadsheet into be copied into a new spreadsheet and save it into specified location in hard drive


Can anyone help me with regards to coding this
0
Comment
Question by:srikanthv2322
6 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34934169
Like this?

Sub Sample()
    Dim wb1 As Workbook, wb2 As Workbook
    
    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks.Add
    
    wb1.Sheets("Sheet1").Range("B5:G20").Copy _
    wb2.Sheets("Sheet1").Range("A1")
    
    wb2.SaveAs "C:\Temp.xlsx"
    wb2.Close
End Sub

Open in new window


Sid
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 34934199
Are you looking for a new workSHEET or a new workBOOK?

Either way, a simply solution can be done without VBA.

If data is on Sheet1, proceed to Sheet2 and enter this formula in B5:

=Sheet1!B5 (or ='OriginalWorkbook!Sheet1'!B5)

The copy that into each desired cell, such as B6, B7...C5...G5...G20
0
 
LVL 2

Expert Comment

by:mscanlon06851
ID: 34935770
The attached workbook demostrates a possible solution.  It's somewhat more generalized than your specifications.  Specifically, it copies whatever range you have selected into a new workbook and lets you save it to disk.  To run the demo:

- open the workbook
- press shift-ctrl-s to launch the "SaveRangeAsWorkbook" macro
- save the resulting file

You can use the attached workbook as a template, or, if you'd like to add the code to your personal macro workbook (personal.xls) so that it will run on any workbook, here's the code to copy/paste into personal.xls:

(Note 1:  if you want to assign this code to a hotkey, you'll need to do it yourself after you paste in the code)

(Note 2: this code assumes that you want to save the file in .xls format.  You'll need to modifythe "Save As" command appropriately if you want to save it in any of the Excel 2007 / 2010 formats)


Sub SaveARangeAsAWorkbook()

    Dim Response As Integer
    Dim Filespec As Variant
   
    Response = _
        MsgBox("Save these cells in a new workbook " & Chr(10) & Chr(10) & Selection.Address & " ?", _
        vbOKCancel, "Save Range as a Workbook")
    
    If Response = vbCancel Then
        Exit Sub
    End If
    
    Selection.Copy
    
    Workbooks.Add
        ActiveSheet.Paste
        
    Filespec = Application.GetSaveAsFilename( _
        FileFilter:="Excel Files,*.xls,All Files,*.*", _
        Title:="Save As File Name")

    If Filespec = False Then
        ActiveWorkbook.Close False
        Exit Sub
    End If

    If LCase$(Right$(Filespec, 4)) <> ".xls" Then
        Filespec = Filespec & ".xls"
    End If
    
    With ActiveWorkbook
        .SaveAs Filename:=Filespec
        .Close
    End With
    
    MsgBox ("Workbook Saved")
    
End Sub

Open in new window

Demo-SaveRangeAsWorkbook.xls
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:srikanthv2322
ID: 34939825
I want the name of output file to be dynamic instead of static, because next time when i run the code, it prompts me with message saying ' file already exists in the specified location, d you want replace it?"

Instead i want the name file as dynamic, like it should end with System Time/Date

EX:
Temp_0822_02202011

Above request was pulled at 08:22 and date 02/20/2011

Can anyone help me with the coding.....
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 34939831
Try this

Replace the line

wb2.SaveAs "C:\Temp.xlsx"

in the code that I gave above with

wb2.SaveAs "C:\Temp" & Format(Now, "ddmmyyhhmmss") & ".xlsx"

Sid
0
 

Author Comment

by:srikanthv2322
ID: 34940048
Thank you so much, Sid
Its working and quite simple coding that i can understand




0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

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