Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Save Specified Contents in Spreadsheet Into New Spreadsheet

Posted on 2011-02-19
6
Medium Priority
?
828 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
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…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

971 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