Solved

Save Specified Contents in Spreadsheet Into New Spreadsheet

Posted on 2011-02-19
6
817 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now