?
Solved

Save Specified Contents in Spreadsheet Into New Spreadsheet

Posted on 2011-02-19
6
Medium Priority
?
829 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

579 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