+ Reply to Thread
Results 1 to 8 of 8

Save info to closed file

Hybrid View

  1. #1
    Registered User
    Join Date
    01-23-2009
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    12

    Save info to closed file

    I want to put the value 6 in cell A1 on sheet1 of a file named book1.xlsm on my desktop. Can I do this without actually opening the file?

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    I could be wrong, but I don't think so. However, using VBA, you can open the book, insert it, then close it, without seeing it open and close.

  3. #3
    Registered User
    Join Date
    01-23-2009
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    12
    Ok cool. I'm looking into how to do this, but any help would be appreciated. Thanks.

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Something like this would work:

    Sub OpenFile()
        Dim strFile As String
        Dim Wkb As Workbook
        
        strFile = "C:\Temp\book1.xlsm"
        
        Application.ScreenUpdating = False  'used so you don't see the file opening
        
        Set Wkb = Workbooks.Open(strFile)
        Wkb.Worksheets(1).Range("A1") = 6
        Wkb.Close SaveChanges:=True
        
        Application.ScreenUpdating = True
    End Sub
    HTH

    Jason

  5. #5
    Registered User
    Join Date
    01-23-2009
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    12
    Thanks. I tried it on my own, and then I tried your way, and both times I get the same error.:

    Run time error '1004'
    Application-defined or object-defined error.

    Any thoughts?

  6. #6
    Registered User
    Join Date
    01-23-2009
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    12
    Ok, somehow I got that one to work, but you can still see the file open for a second and then close. Is there any way to have this all happen behind the scenes?

  7. #7
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764
    Hi
    try the following codes. replace the filepath (G:\macro\) with yours.
    Application.DisplayAlerts = False
    Workbooks.Open Filename:="G:\macro\book1.xlsm"
    Sheets("Sheet1").Cells(1, 1) = 6
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    MsgBox "Data entered in cell A1"
    Ravi

  8. #8
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524
    Quote Originally Posted by toddgak View Post
    Ok, somehow I got that one to work, but you can still see the file open for a second and then close. Is there any way to have this all happen behind the scenes?
    Which One did you get to work, yours or Jason's?

+ 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