+ Reply to Thread
Results 1 to 20 of 20

Close workbook whose name is different each day.

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Question Close workbook whose name is different each day.

    I have a macro that runs daily. It formats and then copies data from one workbook to another. The first workbook, call it A, needs to be closed after I'm done with it, but it's name is different every day. workbook B stays the same except that I'm adding the data from A to it. I've got everything else working just fine except closing A. I try closing A before switching to B, but then it doesn't copy the data. I tried going back to B and using just the first part of its name (since the first part stays the same then it has the date after) and using * to mark for the rest. This did not work either.

    Anyone have any suggestions?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    How are you opening the workbook you want to close?

    If you are using code then create a reference to it when you open it.

    Then you can use that reference in the rest of the code and when you are finished with it to close it.
    Dim wbB As Workbook
    
        Set wbB = Workbooks.Open("C:\ADir\AWorkbook.xlsx")
    
         ' do things with wbB
    
         ' close wbB
         wbB.Close SaveChanges:=False
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: Close workbook whose name is different each day.

    Sub test()
    Dim openbook As Workbook
    For Each openbook In Application.Workbooks
        If Not openbook.Name = "WORKBOOK B NAME HERE" Then openbook.Close False
    Next openbook
    End Sub

  4. #4
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    @Nori - The workbook I'm trying to close is a report that I open from my email.
    @Leo - That won't work as I have several other workbooks open in the same instance that I don't want closed.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    Why not open the 2 workbooks A and B in the same instance of Excel, separate from the instance with multiple workbooks open?

  6. #6
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Is there no way to do with the others in the same instance. I don't know that the other users will always do that. And I always have them open, except for A which is new every day.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    Is there any way to identify the workbook?

    For example, does it's name always contain a certain word/words?

  8. #8
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Yes, it always starts with Storage_Trending_

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    You could use this to close the workbook.
    Dim wb As Workbook
    
         For Each wb In Application.Workbooks
                If wb.Name Like "Storage_Trending*" Then
                        wb.Close SaveChanges:=True
                        Exit For
                End If
         Next wb
    By the way, how are you referring to this workbook in the code that transfers data from it to the other workbook?

  10. #10
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    This is the macro that I use. I start the aSetup and it does the rest. I just added your code to it. It did not however close the workbook.

    Sub DeleteRow()
    '
    ' DeleteRow Macro
    ' Start with the data column and delete the entire row
    '
        'This clears the selected cell
        ActiveCell.FormulaR1C1 = ""
        'this moves to the next column in the row
        ActiveCell.Offset(0, 1).Range("A1").Select
        ActiveCell.FormulaR1C1 = ""
        Call CopyPaste
        ActiveCell.Offset(0, 1).Range("A1").Select
        ActiveCell.FormulaR1C1 = ""
        Call CopyPaste
        ActiveCell.Offset(0, 1).Range("A1").Select
        ActiveCell.FormulaR1C1 = ""
        Call CopyPaste
        'This returns to the first cell that was selected
        ActiveCell.Offset(0, -3).Range("A1").Select
    End Sub
    
    Sub usedGB()
    '
    ' usedGB Macro
    ' Calculates the used % and the daily difference and puts the date in the date column
    '
        'This subtracts the free space from the capacity and tehn divides by the capacity to get the % used
        ActiveCell.FormulaR1C1 = "=(R2C-RC[-1])/R2C"
        Call CopyPaste
        'This goes to the Difference column
        ActiveCell.Offset(0, -2).Range("A1").Select
        'This subtracts the previous days value from todays value
        ActiveCell.FormulaR1C1 = "=RC[2]-R[-1]C[2]"
        Call CopyPaste
        Call SetFormat
    End Sub
    
    Sub ChangeOrder()
    '
    ' ChangeOrder Macro
    ' switches the order of 2 rows
    '
        ActiveCell.Range("A1:C1").Select
        Selection.Cut
        ActiveCell.Offset(0, 4).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(1, -4).Range("A1:C1").Select
        Selection.Cut
        ActiveCell.Offset(-1, 0).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 4).Range("A1:C1").Select
        Selection.Cut
        ActiveCell.Offset(1, -4).Range("A1").Select
        ActiveSheet.Paste
    End Sub
    
    Sub aSetup()
    '
    ' Setup Macro
    ' deletes the extra space at the begining of the list, sets the formatting, changes the order of the 2 that are out of alpahbetical order,
    '   copies the numbers from column B to row 2, and inserts the required number of spaces for copying to the Storage Trending workbook
        ActiveWindow.DisplayGridlines = True
        Range("A3").Select
        Selection.Copy
        Range("D10").Select
        ActiveSheet.Paste
        Selection.NumberFormat = "m/d/yyy"
        Range("A1").Select
        ActiveCell.Range("A1:F7").Select
        Selection.Delete shift:=xlUp
        ActiveCell.Columns("A:C").EntireColumn.Select
        Selection.Style = "Normal"
        Selection.Rows.AutoFit
        Selection.Columns.AutoFit
        ActiveCell.Offset(30, 0).Range("A1").Select
        Call ChangeOrder
        ActiveCell.Offset(5, 0).Range("A1").Select
        Call ChangeOrder
        Call CopyShift
        Call Insert2Spaces
        Call FinishSetup
        Call SetFormat
    End Sub
    
    Sub CopyShift()
    '
    ' CopyShift Macro
    ' copies the contents of a column into a row
    '
        ActiveCell.Offset(11, 2).Range("A1").Select
        Range(Selection, Selection.End(xlUp)).Select
        Selection.Copy
        ActiveWindow.SmallScroll Down:=15
        ActiveCell.Offset(2, 3).Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
        ActiveCell.Offset(0, 1).Range("A1").Select
    End Sub
    
    Sub Insert2Spaces()
    '
    ' Insert2Spaces Macro
    ' inserts to spaces between the numbers to match the format of the Storage Trending workbook
    '
     For k = 0 To 47
        Selection.Insert shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        ActiveCell.Offset(0, 1).Range("A1").Select
        Selection.Insert shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        ActiveCell.Offset(0, 2).Range("A1").Select
    Next k
        ActiveCell.Offset(0, -147).Range("A1").Select
        ActiveCell.Range("A1:EQ1").Select
        Selection.Copy
    End Sub
    
    Sub CopyPaste()
    '
    ' CopyPaste Macro
    ' copies the formula from the first cell to the others in the same row 3 cells apart for 48 times
    ' then goes back to the first cell that was copied
       For k = 0 To 47
        ActiveCell.Select
        Selection.Copy
        ActiveCell.Offset(0, 3).Range("A1").Select
        ActiveSheet.Paste
    Next k
        ActiveCell.Offset(0, -144).Range("A1").Select
        Application.CutCopyMode = False
    End Sub
    
    Sub SetFormat()
    '
    ' SetFormat Macro
    ' This selects all of the filled cells and does an autofit on them so set the formatting
    '
    
        Dim l As Long
        Dim lRow As Long
        
        lRow = Sheets("Data").Range("A1").End(xlDown).Row
        
        Sheets("Info").Select
        Range("A1:H50").Select
        Selection.Columns.AutoFit
        Range("A1").Select
        Sheets("Data").Select
        Range("A1:ER200").Select
        Selection.Columns.AutoFit
        For l = lRow To 1000 Step 1
            If (Range("A" & l).Value) Like "*/20*" Then
                Range("A" & l).Select
            End If
        Next l
    End Sub
    
    Sub FinishSetup()
    '
    'This changes the active workbook and copies everything over to the Storage Trending workbook
    'Then it processes the newly copied data and formats everything
    
        Dim l As Long
        Dim lRow As Long
        Dim wb As Workbook
        
        Selection.Copy
        Windows("Storage Trending").Activate
        Range("A1").Select
        lRow = Sheets("Data").Range("A1000").End(xlUp).Row
    
        For l = lRow To 5 Step -1
        Range("A" & l).Select
    '   Checking for each line till it finds the first empty one
            If (Range("A" & l).Value) Like "*" Then
                If Range("A" & l + 1).Value Like "" Then
                    Range("A" & l + 1).Select
                    Selection.PasteSpecial (xlPasteValues)
                    ActiveCell.Offset(0, 3).Range("A1").Select
                    Call usedGB
                End If
            End If
        Next l
        Range("A1").Select
        For Each wb In Application.Workbooks
            If wb.Name Like "Storage_Trending_*" Then
                wb.Close savechanges:=True
                Exit For
            End If
        Next wb
    End Sub

  11. #11
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    I don't refer to it. I open it from my email, which opens it in the same instance as the others. Then I am able to run the macros that I have on the other workbooks that are open. I run a setup macro that formats the data then changes the focus to the appropriate workbook to copy it to. I just wanted to close the first workbook as it is not needed once the data is copied over. I'll try your code out to see what happens.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    You can't copy/format a workbook without referring to it in some way.

    Does your code assume that the active workbook is the correct workbook?

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    The code I posted will close the first workbook it comes across with a name beginning 'Storage_Trending'.

    Are you sure the name begins with that? No spaces, weird characters, misspelling...

    PS In your code you are referring to a workbook called 'Storage Trending', is the workbook you are copying to?

  14. #14
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Yeah, the name always starts with that, it's the name of the report. Then it has the days date after. There are no spaces the report uses '_' instead of spaces. I ran your code and added breakpoints to watch what it does and it never goes into the 'IF' statement as though none of the workbooks have a name starting with Storage_Trending_

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    Add this to the code just before the If
    Debug.Print wb.Name
    Then run the code, open the immediate window (CTRL+G) and you should see a list of all the open workbooks.

  16. #16
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    The name is in all caps for the workbook that I'm trying to close does that matter? It does show up in the window as the last workbook that is looked at.

  17. #17
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    I did that and I see it run through all the open workbooks that I have. I saw that the workbook I'm trying to close is in all caps so I changed the code to reflect that and now it tries to close the workbook, but wants to save it first. How do I prevent it from trying to save?

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Close workbook whose name is different each day.

    Yes it matters, a lot.

    Try this, it converts both the worksheet name and what we are looking for to upper case, so case shouldn't be an issue.
    For Each wb In Application.Workbooks
       If UCase(wb.Name) Like UCase("Storage_Trending_*") Then
          wb.Close SaveChanges:=False
          Exit For
       End If
    Next wb

  19. #19
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Awesome, that works! Thanks for all the help!

  20. #20
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: Close workbook whose name is different each day.

    if you are opening it from an email attachment it won't have a save location right? so how about:
    Sub test()
    Dim openbook As Workbook
    For Each openbook In Application.Workbooks
        If openbook.Path = "" Then openbook.Close False
    Next openbook
    End Sub

+ 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. [SOLVED] Closing a Workbook from another workbook: Workbooks.close error: Subscript out of range
    By Coreyusa in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-14-2013, 06:45 PM
  2. Disable close workbook but still force users to close thru command button
    By rathig in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2013, 07:29 AM
  3. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  4. [SOLVED] Edit code to close all workbooks except active workbook and other specific workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 09:29 PM
  5. How to open,save and close another workbook from the current workbook using macro?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2012, 10:42 AM

Tags for this Thread

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