Closed Thread
Results 1 to 2 of 2

VBA Create new workbook and copy paste data

  1. #1
    Registered User
    Join Date
    09-05-2014
    Location
    canada
    MS-Off Ver
    2010
    Posts
    5

    VBA Create new workbook and copy paste data

    I posted this in general discussion but I noticed that it would be more appropriate to put it here.

    I have a set of data in one workbook which I'll call Workbook1 for instance.

    I've attached a test spreadsheet which I'm using to try and replicate the effect but with less complex data.
    TestSource.xlsx

    I would like to create a macro (an inputbox maybe) to search for a region (A for example) and create a new workbook & save with the region's name (A.xls for example). The more complicated part would be to then copy the information to the new workbook from columns "office - total" and all the rows from office AA to total. I tried to figure this out searching through the forums but couldn't quite put everything together. What i'm trying to aim at is to be able to input a region in the source workbook and automatically create a new workbook with the inputted region. The new workbook should then be automatically populated with information under the region from the source workbook.

    I tried to develop a VBA for this and so far this is what I have:

    Private Sub CommandButton1_Click()

    Dim MP As String
    Dim Rng As Range
    Dim lastRow As Long
    Dim lastRowDest As Long
    Dim caddress As String
    Dim oldbook As Workbook
    Dim newbook As Workbook


    Application.ScreenUpdating = False


    Set oldbook = ThisWorkbook


    On Error GoTo errorM

    MP = Application.InputBox("Enter Region")
    If MP = "" Then Exit Sub
    If Trim(MP) <> "" Then

    Columns("B:B").Select
    Selection.Find(What:=MP, After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, _
    SearchFormat:=False).Activate

    ActiveCell.Select
    caddress = ActiveCell.Offset(0, -1).Address(False, False)

    '/Create new workbook & save
    Set newbook = Workbooks.Add


    '/ copies headers to new workbook


    oldbook.Sheets("sheet1").Range("B1:G1").Copy newbook.Sheets("sheet1").Range("A1")

    With oldbook.Sheets("Sheet1")
    lastRow = .Range("B" & Rows.Count).End(xlUp).Row
    .Range(caddress & ":G" & lastRow).Copy Destination:= _
    newbook.Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp)(2)

    End With
    newbook.SaveAs Filename:="C:\" & MP & ".xls"
    Application.ScreenUpdating = True
    MsgBox "Done!"



    End If
    Exit Sub
    errorM:
    MsgBox ("Invalid Region")

    End Sub

    So far what it does is create a new workbook with the inputted name. It then copies all the data from the matching name in the source document. I'm still trying to figure out how to make it stop at the Total, and also to make the columns copied flexible based on the number of columns I have. Any ideas on how to do this? Any help would be much appreciated!

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,882

    Re: VBA Create new workbook and copy paste data

    See my response in your earlier post.

    Welcome to the Forum, unfortunately:

    This is a duplicate post and as such does not comply with Rule 5 of our forum rules. This thread will now be closed, you may continue in your other thread.


    Additionally,

    Your post does not comply with the rules governing code tags. Please read all the rules and comply with them in the future.

    Thread Closed.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Create new workbook and copy paste data
    By jayt91 in forum Excel General
    Replies: 8
    Last Post: 09-05-2014, 09:46 PM
  2. Replies: 6
    Last Post: 01-29-2013, 07:01 AM
  3. [SOLVED] create workbook and paste data and cut paste columns
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-17-2012, 04:22 AM
  4. Copy data from workbook, create new workbook, paste data to new worbook?
    By mpkavanagh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-16-2011, 06:39 PM
  5. Replies: 1
    Last Post: 10-17-2005, 04:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1