+ Reply to Thread
Results 1 to 11 of 11

Making a macro run automatically and update data in a sheet (not create a new one)

  1. #1
    Registered User
    Join Date
    07-11-2013
    Location
    Preston, Lancashire
    MS-Off Ver
    Excel 2013
    Posts
    11

    Making a macro run automatically and update data in a sheet (not create a new one)

    Hi! I'm new to all this so sorry if this is a daft question. I have a macro that can be used to show the date various word files are updated. However the only way I can get it to work is by rerunning the macro which then places all the updated information a new sheet. Is there anyway that I can make it so that the macro runs as soon as I open the sheet and so that it always updates to the same place (and therefore doesn't create a new sheet)?

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    Yes there is probably a way. It might be easier if you attached what you have so far to be edited.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    07-11-2013
    Location
    Preston, Lancashire
    MS-Off Ver
    Excel 2013
    Posts
    11

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    Public Sub GetFileInfo()
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim wksLog As Excel.Worksheet
    Dim lngRow As Long: lngRow = 1

    Const strPath As String = "C:\Users\User\Dropbox\TSCat GEN\Pipeline\Projects and Project Ideas\Project Overviews"
    Const strFileExtension As String = "*.doc*"

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFolder = objFSO.GetFolder(strPath)
    For Each objFile In objFolder.Files
    With objFile
    If .Name Like strFileExtension Then
    If wksLog Is Nothing Then
    Set wksLog = ThisWorkbook.Sheets.Add
    wksLog.Range("A1:C1").Value = VBA.Array("File name", "Date created", "Date last modified")
    End If
    lngRow = lngRow + 1
    wksLog.Cells(lngRow, "A").Value = .Name
    wksLog.Cells(lngRow, "B").Value = .DateCreated
    wksLog.Cells(lngRow, "C").Value = .DateLastModified
    End If
    End With
    Next objFile
    End Sub

  4. #4
    Registered User
    Join Date
    07-11-2013
    Location
    Preston, Lancashire
    MS-Off Ver
    Excel 2013
    Posts
    11

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    That's what I've got so far.

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    Try changing this:
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    Also to get it to run automatically you'll have to copy everything (Except the very first and very last lines) into the workbook_open event

  6. #6
    Registered User
    Join Date
    07-11-2013
    Location
    Preston, Lancashire
    MS-Off Ver
    Excel 2013
    Posts
    11

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    I've done the first thing you recommend and it worked a treat. Thank you. However as I know so little the second recommendation means nothing to me. What is the 'workbook_open event.' I'm completely new to all this and will need quite a bit of detail.

  7. #7
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    Capture.jpg


    See attached picture. I've highlighted the areas you need to change. Double click Workbook (highlighted in box to left) and then change the boxes highlighted across the top to their respective values.

    Please Login or Register  to view this content.
    Will populate automatically. This is why its important to delete the first and last lines of the code you have currently. Paste the rest in between Sub and end sub.

  8. #8
    Registered User
    Join Date
    07-11-2013
    Location
    Preston, Lancashire
    MS-Off Ver
    Excel 2013
    Posts
    11

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    I've tried making mine look the same as yours but it still doesn't seem to update automatically. Here is the code as I have it
    Private Sub Workbook_Open()
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim wksLog As Excel.Worksheet
    Dim lngRow As Long: lngRow = 1

    Const strPath As String = "C:\Users\User\Dropbox\TSCat GEN\Pipeline\Projects and Project Ideas\Project Overviews"
    Const strFileExtension As String = "*.doc*"

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFolder = objFSO.GetFolder(strPath)
    For Each objFile In objFolder.Files
    With objFile
    If .Name Like strFileExtension Then
    If wksLog Is Nothing Then
    Set wksLog = ActiveSheet
    wksLog.Range("A1:C1").Value = VBA.Array("File name", "Date created", "Date last modified")
    End If
    lngRow = lngRow + 1
    wksLog.Cells(lngRow, "A").Value = .Name
    wksLog.Cells(lngRow, "B").Value = .DateCreated
    wksLog.Cells(lngRow, "C").Value = .DateLastModified
    End If
    End With
    Next objFile
    End Sub

  9. #9
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    Michael,

    When post code, please press the # button above and paste the code in between [code] and [\code].

    Are you getting an error? Have the files changed so that there is actually anything to update?

  10. #10
    Registered User
    Join Date
    07-11-2013
    Location
    Preston, Lancashire
    MS-Off Ver
    Excel 2013
    Posts
    11

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    It works! Thank you very much!

  11. #11
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Making a macro run automatically and update data in a sheet (not create a new one)

    No problem.

    If you would, please take the time mark the thread as [SOLVED] and add reputation by clicking the * below.

    Thanks,

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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