+ Reply to Thread
Results 1 to 7 of 7

Automatically open, close & saved documents

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-29-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    106

    Automatically open, close & saved documents

    Hi all,

    I need your assistance to help me to open, close & saved a database file.
    Below is the VB Script ,
    Thank you in advance

    Sub UpdateLogWorksheet()
    
        Dim historyWks As Worksheet
        Dim inputWks As Worksheet
    
        Dim nextRow As Long
        Dim oCol As Long
    
        Dim myRng As Range
        Dim myCopy As String
        Dim myCell As Range
        
        'cells to copy from database sheet - some contain formulas
        myCopy = "P1,E1,E2,E3,E4,E5,AI2,W3,W4,W5,W6,E6,P2,P3,P4,P5,AF5,AF6,AJ6"
    
        Set inputWks = Worksheets("source")
        Set historyWks = Workbooks("database.xlsx").Worksheets("data")
    
        With historyWks
            nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
        End With
    
        With inputWks
            Set myRng = .Range(myCopy)
    
            If Application.CountA(myRng) <> myRng.Cells.Count Then
                MsgBox "Please fill in all the YELLOW coloured cells!"
                Exit Sub
            End If
        End With
    
        With historyWks
            With .Cells(nextRow, "A")
                .Value = Now
                .NumberFormat = "dd/mm/yyyy hh:mm:ss"
            End With
            .Cells(nextRow, "B").Value = Application.UserName
            oCol = 3
            For Each myCell In myRng.Cells
                historyWks.Cells(nextRow, oCol).Value = myCell.Value
                oCol = oCol + 1
            Next myCell
        End With
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Automatically open, close & saved documents

    Hi,

    The following will probably do what you want, but has no error checking.
    Sub SimpleOpenSaveCloseFileWithNoErrorChecking()
        
        Dim sFileName As String
        Dim sPath As String
        Dim sPathAndFileName As String
        
        'Build a File Name (i.e. Worksheet name) and 'Path and File Name'
        'Assume the 'Workbook' is in the same folder as the file running the code
        sPath = ThisWorkbook.Path & "\"
        sFileName = "database.xlsx"
        sPathAndFileName = sPath & sFileName
        
        'Disable macros in the file being opened (optional)
        Application.EnableEvents = False
    
        Workbooks.Open Filename:=sPathAndFileName
        
        'Reenable Macros
        Application.EnableEvents = True
        
        
        'Save file and keep open
        Workbooks(sFileName).Save
    
        'or Save the File and Close
        Workbooks(sFileName).Close SaveChanges:=True
        
        'or Close the file and don't save
        Application.DisplayAlerts = False     'Inhibit 'Do you really want to do this ...' question
        Workbooks(sFileName).Close SaveChanges:=False
        Application.DisplayAlerts = True
        
    End Sub
    For an example of some of the same code with extensive error checking see post #3 or post #5 from http://www.excelforum.com/excel-gene...ta-weekly.html

    Lewis

  3. #3
    Forum Contributor
    Join Date
    11-29-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    106

    Re: Automatically open, close & saved documents

    Dear Lewis,

    Thank you for your reply, but
    can this script combined with my "updatelogworksheet"? because I want the
    file open & after update it'll closed & saved by itself... thanks again

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Automatically open, close & saved documents

    namialus,

    Try this. It opens the file, runs your macro code, then saves and closes the file.

    Lewis

    Sub UpdateLogWorksheet()
    
        Dim historyWks As Worksheet
        Dim inputWks As Worksheet
    
        Dim nextRow As Long
        Dim oCol As Long
    
        Dim myRng As Range
        Dim myCopy As String
        Dim myCell As Range
        
        Dim sHistoryFileName As String
        Dim sPath As String
        Dim sPathAndFileName As String
        
        'Build a File Name (i.e. Worksheet name) and 'Path and File Name'
        'Assume the 'Workbook' is in the same folder as the file running the code
        'Change the path if your path is DIFFERENT
        sPath = ThisWorkbook.Path & "\"
        sHistoryFileName = "database.xlsx"
        sPathAndFileName = sPath & sHistoryFileName
        
        'Open the data file
        Workbooks.Open Filename:=sPathAndFileName
        
        myCopy = "P1,E1,E2,E3,E4,E5,AI2,W3,W4,W5,W6,E6,P2,P3,P4,P5,AF5,AF6,AJ6"
    
        Set inputWks = Worksheets("source")
        Set historyWks = Workbooks("database.xlsx").Worksheets("data")
        
        'You should probably change the previous line to:
        Set historyWks = Workbooks(sHistoryFileName).Worksheets("data")
        
        
    
        With historyWks
            nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
        End With
    
        With inputWks
            Set myRng = .Range(myCopy)
    
            If Application.CountA(myRng) <> myRng.Cells.Count Then
                MsgBox "Please fill in all the YELLOW coloured cells!"
                Exit Sub
            End If
        End With
    
        With historyWks
            With .Cells(nextRow, "A")
                .Value = Now
                .NumberFormat = "dd/mm/yyyy hh:mm:ss"
            End With
            .Cells(nextRow, "B").Value = Application.UserName
            oCol = 3
            For Each myCell In myRng.Cells
                historyWks.Cells(nextRow, oCol).Value = myCell.Value
                oCol = oCol + 1
            Next myCell
        End With
        
        'Save the File and Close
        Workbooks(sHistoryFileName).Close SaveChanges:=True
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    11-29-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    106

    Re: Automatically open, close & saved documents

    Dear Lewis,

    I received error "run-time error 9 ; subscript out of range"
    & yellow mark at script line :

    Set inputWks = Worksheets("source")

    Note : The database file opened but no input ...

    Thank you again

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Automatically open, close & saved documents

    I didn't test the macro the first time because I didn't think anything could go wrong, go wrong, go wrong. I was misinformed. I apologize.

    I had to move the item in blue to the beginning of the macro. The macro was looking for Sheet 'source' in the wrong workbook.

    Try again with:
    Sub UpdateLogWorksheet()
    
        Dim historyWks As Worksheet
        Dim inputWks As Worksheet
    
        Dim nextRow As Long
        Dim oCol As Long
    
        Dim myRng As Range
        Dim myCopy As String
        Dim myCell As Range
        
        Dim sHistoryFileName As String
        Dim sPath As String
        Dim sPathAndFileName As String
        
        'Set the Input Worksheet object (from the Active Workbook)
        Set inputWks = Worksheets("source")
        
        'Build a File Name (i.e. Worksheet name) and 'Path and File Name'
        'Assume the 'Workbook' is in the same folder as the file running the code
        'Change the path if your path is DIFFERENT
        sPath = ThisWorkbook.Path & "\"
        sHistoryFileName = "database.xlsx"
        sPathAndFileName = sPath & sHistoryFileName
        
        'Open the data file
        Workbooks.Open Filename:=sPathAndFileName
        
        myCopy = "P1,E1,E2,E3,E4,E5,AI2,W3,W4,W5,W6,E6,P2,P3,P4,P5,AF5,AF6,AJ6"
    
        Set historyWks = Workbooks("database.xlsx").Worksheets("data")
        
        'You should probably change the previous line to:
        Set historyWks = Workbooks(sHistoryFileName).Worksheets("data")
        
        
    
        With historyWks
            nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
        End With
    
        With inputWks
            Set myRng = .Range(myCopy)
    
            If Application.CountA(myRng) <> myRng.Cells.Count Then
                MsgBox "Please fill in all the YELLOW coloured cells!"
                Exit Sub
            End If
        End With
    
        With historyWks
            With .Cells(nextRow, "A")
                .Value = Now
                .NumberFormat = "dd/mm/yyyy hh:mm:ss"
            End With
            .Cells(nextRow, "B").Value = Application.UserName
            oCol = 3
            For Each myCell In myRng.Cells
                historyWks.Cells(nextRow, oCol).Value = myCell.Value
                oCol = oCol + 1
            Next myCell
        End With
        
        'Save the File and Close
        Workbooks(sHistoryFileName).Close SaveChanges:=True
        
    End Sub
    Lewis

  7. #7
    Forum Contributor
    Join Date
    11-29-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    106

    Re: Automatically open, close & saved documents

    Dear Lewis,

    The macro working , thank you very much

+ 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. Look for and Open saved workbook retrieve information into new WB then close them
    By Aeneren in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-27-2012, 10:42 PM
  2. Macro to close down word and re-open a previously saved document
    By snoopy1461 in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2011, 09:59 AM
  3. Save an open workbook, then open template workbook and close the saved workbook
    By ondvirg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2009, 10:20 PM
  4. [SOLVED] Open saved projects automatically?
    By Stuhump3 in forum Excel General
    Replies: 1
    Last Post: 09-09-2005, 03:05 PM
  5. How do I automatically open linked documents in Excel?
    By DroKoz in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 06-28-2005, 12:05 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