+ Reply to Thread
Results 1 to 3 of 3

Paste range of formulas to a sheet using Personal Macro Workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    08-31-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    51

    Paste range of formulas to a sheet using Personal Macro Workbook

    I have a spread sheet, and I have a range of formulas on that sheet. I recently made adjustments to the formulas but there are a ton of one off versions of that worksheet. I want to be able to open any one off version of that workbook, run the macro, and my updated range of formulas will be pasted into that old workbook. But I am using the Personal macro file so that I don't have to open up the new file every time.

    So basically, my plan is to paste my new range of formulas onto my personal macro worksheet. Then create a code so that when I open up one of my old workbooks that have outdated formulas, I will run the macro and the new formulas that I have as a range in the personal macro sheet, will be copied and pasted into a specific sheet on the old workbook I have open.

    How would I go about doing this?

    EDIT*** To clarify, I know how to use the personal macro worksheet. My question is strictly about the code for the macro.
    Last edited by Cheeseburger; 06-16-2015 at 12:18 AM.

  2. #2
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: Paste range of formulas to a sheet using Personal Macro Workbook

    Why don't you loop through all the excel files automatically?
    Just put them all in a folder and use this code.

    I allredy posted this another thread today, and he got it working.
    Just change the folder where all you excel files is, and then change the Mymacro at the bottom to make the changes you need.
    This code will then loop through all the files, make the changes and save them.


    Public Function IsFileOpen(strFileName As String) As Boolean
        
        On Error Resume Next 'Ignore any errors (i.e. if workbook is not open)
        
            Set wrkFileName = Workbooks(strFileName)
            
                If wrkFileName Is Nothing Then
                    IsFileOpen = False
                Else
                    IsFileOpen = True
                End If
                
        On Error GoTo 0 'Nullify above error handler
        
    End Function
    Sub Macro1()
    
        Dim strDir As String, _
            strFileType As String
        Dim objFSO As Object, _
            objFolder As Object, _
            objFile As Object
        Dim intCounter As Integer
            
        strDir = "C:\" 'Change to suit
        strFileType = "xl*" 'Shouldn't need to, but change to suit if required
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(strDir)
        
        Application.ScreenUpdating = False
            
        For Each objFile In objFolder.Files
            'If the file in the 'strDir' directory is not this workbook, then...
            If objFile.Name <> ThisWorkbook.Name Then
                If objFile.Name Like "*." & strFileType Then
                    '...check to see if it's open.  If it is...
                    If IsFileOpen(objFile.Name) = True Then
                        '...run the 'MyMacro' passing the active workbook variable with it and _
                        increment the counter.
                         Call MyMacro(objFile.Name)
                         intCounter = intCounter + 1
                    'Else, _
                    1. Open the file, _
                    2. Run the 'MyMacro' passing the active workbook variable with it, _
                    3. Save the changes and close the file, and _
                    4. Increment the counter.
                    Else
                         Workbooks.Open (strDir & "\" & objFile.Name), UpdateLinks:=False
                         Call MyMacro(objFile.Name)
                         Workbooks(objFile.Name).Close SaveChanges:=True
                         intCounter = intCounter + 1
                    End If
                End If
            End If
            'Release memory
            Set objFSO = Nothing
            Set objFolder = Nothing
            Set objFile = Nothing
        Next objFile
        
        Application.ScreenUpdating = True
        
        Select Case intCounter
            Case Is = 0
                MsgBox "There were no """ & strFileType & """ file types in the """ & strDir & """ directory for the desired macro to be run on.", vbExclamation, "Data Execution Editor"
            Case Is = 1
                MsgBox "The desired macro has been run on the only """ & strFileType & """ file in the """ & strDir & """ directory.", vbInformation, "Data Execution Editor"
            Case Is > 1
                MsgBox "The desired macro has now been run on the " & intCounter & " files in the """ & strDir & """ directory.", vbInformation, "Data Execution Editor"
        End Select
        
    End Sub
    
    Sub MyMacro(strDesiredWkb As String)
    
    'Add the code what you want changed in the workbooks here
    
    End Sub

  3. #3
    Registered User
    Join Date
    08-31-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Paste range of formulas to a sheet using Personal Macro Workbook

    Unfortunately, I have to do it by the method that I described for several reasons. One of which is I have to decide which files I want to run the macro in, as sometimes I may want to use the old formulas. Also, I share my personal macro file with a colleague as we both use the same macros for our work.

+ 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. Macro to copy formulas from multiple workbooks into new workbook (paste text of formula)
    By robertsfd2002 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-24-2014, 12:46 PM
  2. Replies: 1
    Last Post: 05-02-2014, 12:19 PM
  3. Copy A Range of Cells Or Sheet and Paste to A New Workbook
    By Nathan123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2007, 09:24 PM
  4. Personal macro workbook and personal.xls
    By John Kilkenny in forum Excel General
    Replies: 1
    Last Post: 06-14-2005, 05:05 PM
  5. Replies: 2
    Last Post: 03-21-2005, 08:06 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