+ Reply to Thread
Results 1 to 7 of 7

Create 10 similar tables with Excel VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-07-2012
    Location
    Europe
    MS-Off Ver
    Excel 2010
    Posts
    53

    Smile Create 10 similar tables with Excel VBA

    Hi all.
    This is my case:
    1. I have a calculation Excel file with data for 10 partners.
    1.1. I have Excel file with 4 tabs.
    2. I need to change the partner number in one tab, in order to calculate the data in the other 3 tabs.
    3. Then I need to export the three tabs with a new name (the one of the partners) in a new file.
    4. This should be repeated for every partner.

    In general, I can write a slow macro with macro recorder, which does exactly this. But the problem is, that I should later make this for 100 partners and it would last about 3-4 minutes pro partner to create, calculate, save and close their worksheets... And 400 minutes is about 4 times more than I would like to wait.
    So, any practical ideas would be appreciated (I am aware how to stop & start calculations and animation, so you can skip these ones).

    And have a nice day!
    Last edited by kaligad; 11-14-2013 at 06:39 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Create 10 similar tables with Excel VBA

    Hi there,

    Can you post the macro that you're currently using?

    Is it possible for you also to post a either a full or a partial typical worksheet?

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    11-07-2012
    Location
    Europe
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Create 10 similar tables with Excel VBA

    Hi
    Actually the macro is really a long one... (with macro recorder and it can be optimized a lot...), but I do not have time || knowledge )

    So, in general, I was thinking about a code, doing the following:

    1. Copy 3 tabs to a new file.
    2. Close the file.


    Consider the names of the tabs as AA,BB,CC.
    Consider the name of the new file as 1-123.

    Any ideas?
    Sorry for not sharing my macro, but it is useless(99% written with macro recorder) and probably some of my colleagues may say that it is confidential...

  4. #4
    Registered User
    Join Date
    11-07-2012
    Location
    Europe
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Create 10 similar tables with Excel VBA

    Actually, I have managed to do something on this request -

        sourcewb.Sheets(Array("AA", "BB", "CC")).Copy
    
        Set wb = ActiveWorkbook
    
        With wb
            .SaveAs ThisWorkbook.Path & "\" & ExcelFileName
        End With
        
        With wb
             .Close False
        End With
    Now I am trying to save it as values... Any ideas? (also for improvement... for example how to make it with a loop and array, in stead of hard coding the tabs?)
    Last edited by kaligad; 11-13-2013 at 12:46 PM.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Create 10 similar tables with Excel VBA

    Hi again,

    Try the following code:

    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub CreateNewWorkbook()
    
        Const sNEW_FILE_NAME    As String = "1-123.xls"
    
        Dim rRangeToCopy        As Range
        Dim vaDataValues        As Variant
        Dim iErrorNumber        As Integer
        Dim wbkTarget           As Workbook
        Dim wks                 As Worksheet
    
        mvaWorksheetsToCopy.Copy
    
        Set wbkTarget = ActiveWorkbook
    
    '   Replace the contents of each worksheet with the values it contains
        For Each wks In wbkTarget.Sheets
    
            With wks
    
                Set rRangeToCopy = Range(.Cells(1, 1), _
                                         .UsedRange.Cells(.UsedRange.Cells.Count))
    
                vaDataValues = rRangeToCopy.Value
                rRangeToCopy.Value = vaDataValues
    
            End With
    
        Next wks
    
        With wbkTarget
    
            On Error Resume Next
                .SaveAs Filename:=ThisWorkbook.Path & "\" & sNEW_FILE_NAME
                iErrorNumber = Err.Number
            On Error GoTo 0
    
            .Close SaveChanges:=False
    
            If iErrorNumber = 0 Then
                  MsgBox "The new workbook has been created and saved", vbInformation
            Else: MsgBox "An error occurred - the new workbook was NOT saved", vbExclamation
            End If
    
        End With
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Function mvaWorksheetsToCopy() As Variant
    
        Const sSHEET_NAMES      As String = "AA,BB,CC"  '   No spaces after commas!!!
        Const sCOMMA            As String = ","
    
        Dim vaWorksheetsToCopy  As Variant
    
        Set vaWorksheetsToCopy = Sheets(Split(sSHEET_NAMES, sCOMMA))
    
        Set mvaWorksheetsToCopy = vaWorksheetsToCopy
    
    End Function

    You'll see that the names of the worksheets to be copied are defined as a Constant in the "mvaWorksheetsToCopy" function:

    
    Const sSHEET_NAMES      As String = "AA,BB,CC"  '   No spaces after commas!!!

    It's important to note that there should be no blank spaces between the separating commas and the sheet names.

    The above code will copy the required worksheets to a new workbook, replace formula results in each worksheet with the corresponding calculated values, save the new workbook, close it, and then generate a "Task Completed" message.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  6. #6
    Registered User
    Join Date
    11-07-2012
    Location
    Europe
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Create 10 similar tables with Excel VBA

    Thank You!
    The only thing I have added is:

    - Spreadsheet calculation
            For Each myWS In ThisWorkbook
                    If ws.Visible Then ws.Select (False)
            Next
            Calculate
    A loop for more files to be generated and the standard:
        Application.Calculation = xlCalculationManual
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    Honestly, Greg M, you are GOOD!

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Create 10 similar tables with Excel VBA

    Hi again,

    Many thanks for your feedback, and especially for your compliment at the end!

    I'm very pleased that I was able to help.

    Best regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 03-05-2020, 11:37 AM
  2. Query to pull from Multiple similar tables
    By Lewis2010 in forum Access Tables & Databases
    Replies: 1
    Last Post: 05-17-2011, 02:40 PM
  3. Create tables in Excel
    By ebbo in forum Excel General
    Replies: 0
    Last Post: 04-20-2009, 12:22 PM
  4. Comparing 2 tables with similar content
    By Vic1978 in forum Excel General
    Replies: 5
    Last Post: 12-05-2005, 04:10 PM

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