Solved

Save Specified Contents in Spreadsheet Into New Spreadsheet

Posted on 2011-02-19
6
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

729 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