+ Reply to Thread
Results 1 to 13 of 13

How to name opened workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    08-25-2016
    Location
    USA
    MS-Off Ver
    14.0.7172.5000 (32-bit)
    Posts
    96

    How to name opened workbooks

    I figured out how to open five workbook, count the number of rows, close them, and print out the results.

    To do this, I only opened one workbook at a time, set it as active, saved the count in a variable, and closed the workbook.

    What I want to do now, though, is make sure the dates align on every row. I want to do this with all five workbooks open at once. To do this, don't I have to give each workbook a name? If so, then how do I assign the name and do I have to dim that name?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to name opened workbooks

    Each workbook already has a name, e.g., Bob.xlsx, referenced as Workbooks("Bob.xlsx").xlsx.

    You could create as array:

    dim aWkb() as workbook
    
    set aWkb(1) = workbooks("Bob.xlsx")
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    08-25-2016
    Location
    USA
    MS-Off Ver
    14.0.7172.5000 (32-bit)
    Posts
    96

    Re: How to name opened workbooks

    Okay. I was on the right track... here's my code thus far:

    Option Explicit
    Option Base 1
    
    Sub CompareDatesAcrossWorkbooks()
    
    Dim Filename(5) As String
    'CLfn As String, GCfn As String, USfn As String, ECfn As _
    String, ESfn As String
    'Dim CLrc As Integer, GCrc As Integer, USrc As Integer, ECrc _
    As Integer, ESrc As Integer 'These are number of rows in each file
    Dim tWB As Workbook, aWB As Workbook
    Dim i As Integer, j As Integer, match As Integer, nomatch As _
    Integer, nr As Integer
    Dim Workbook(5) As Workbook
    
    Set tWB = ThisWorkbook
    
    Columns("A:D").ClearContents
    
    Filename(1) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\CLdata.csv"
    Filename(2) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\GCdata.csv"
    Filename(3) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\USdata.csv"
    Filename(4) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\ECdata.csv"
    Filename(5) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\ESdata.csv"
    
    match = 0
    nomatch = 0
    
    For i = 1 To 5:
        Workbooks.Open Filename(i)
        Workbook(i) = Filename(i)
        'Set aWB = Workbook(i)
    Next i
    
    Range("A1").CurrentRegion.Select
    nr = Selection.Rows.Count
    
    For j = 2 To nr:
        If Workbook(1).Cells(j, 1) = Workbook(2).Cells(j, 1) And _
        Workbook(2).Cells(j, 1) = Workbook(3).Cells(j, 1) And _
        Workbook(3).Cells(j, 1) = Workbook(4).Cells(j, 1) And _
        Workbook(4).Cells(j, 1) = Workbook(5).Cells(j, 1) Then
            match = match + 1
        Else
            nomatch = nomatch + 1
        End If
    Next j
    
    For k = 1 To 5:
        Workbook(i).Close SaveChanges:=False
    Next k
    
    Range("A1").Value = match 'Doesn't matter whether .Value is at the end
    Range("A2") = nomatch
    
    Range("B1") = "dates matched across all five WBs"
    Range("B2") = "dates did not match across all five WBs"
    
    End Sub
    In the "For i" loop, as written I get an "object variable or with block variable not set." If I comment out the second line and uncomment out the third line, though, then it works. Why?

    Also in the "For j" loop, I get the same "object variable or with block variable not set" on the long compound if line (four conditions). Why?

    Thank you!

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to name opened workbooks

    I'd start like this:

    Option Explicit
    
    Sub CompareDatesAcrossWorkbooks()
      Const sPath       As String = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\"
      Dim aWkb(1 To 5)  As Workbook
      Dim avsFile       As Variant
    
      Dim tWkb          As Workbook
      Dim i             As Long
    
      avsFile = [{"CLdata.csv","GCdata.csv","USdata.csv","ECdata.csv","ESdata.csv"}]
    
      For i = 1 To 5
        Set aWkb(i) = Workbooks.Open(sPath & avsFile)
      Next i
    
      ' now they're all open ...
    End Sub

  5. #5
    Registered User
    Join Date
    08-25-2016
    Location
    USA
    MS-Off Ver
    14.0.7172.5000 (32-bit)
    Posts
    96

    Re: How to name opened workbooks

    I appreciate the brevity of your version. I didn't know you could combine sPath & avsFile like that.

    What about mine is giving the "object variable or with block variable not set" error? I can't see what I failed to initialize. If it's something about my not understanding details of the objects then I really want to learn so I don't make such mistakes in the future.

    Just to clarify, with regard to the "For i" loop if I comment out the second line and uncomment out the third line, then it works and all five workbooks open. I still run into that same error in the "For j" loop, though.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to name opened workbooks

    1. Workbook is a data type in the Excel object model. You don't want to use it as a variable name.

    2. Object variables require the Set keyword.

  7. #7
    Registered User
    Join Date
    08-25-2016
    Location
    USA
    MS-Off Ver
    14.0.7172.5000 (32-bit)
    Posts
    96

    Re: How to name opened workbooks

    Quote Originally Posted by shg View Post
    1. Workbook is a data type in the Excel object model. You don't want to use it as a variable name.

    2. Object variables require the Set keyword.
    I replaced Workbook() with Asset().

    I also set aWB = ActiveWorkbook.

    I'm still getting errors in the same places. One thing I'm noticing in the Locals window is that when a workbook opens...

        Set Asset(i) = aWB
    ...is not assigning the newly opened workbook to Asset(i). When a workbook opens, isn't it active by default? If not, then how do I assign the newly-opened workbook to the Asset array? I tried with the line

    Set Asset(i) = Filename(i)
    but this results in a mismatch error (I'm guessing because the former is an array of workbooks and the latter is an array of strings).

    Thanks!

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to name opened workbooks

    Post your code, Mark.

  9. #9
    Registered User
    Join Date
    08-25-2016
    Location
    USA
    MS-Off Ver
    14.0.7172.5000 (32-bit)
    Posts
    96

    Re: How to name opened workbooks

    Quote Originally Posted by shg View Post
    Post your code, Mark.
    Sub CompareDatesAcrossWorkbooks()
    
    Dim Filename(5) As String
    Dim tWB As Workbook, aWB As Workbook
    Dim i As Integer, j As Integer, match As Integer, nomatch As _
    Integer, nr As Integer, k As Integer
    Dim Asset(5) As Workbook
    
    Set tWB = ThisWorkbook
    Set aWB = ActiveWorkbook 'Forgot this line
    
    Columns("A:D").ClearContents
    
    Filename(1) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\CLdata.csv"
    Filename(2) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\GCdata.csv"
    Filename(3) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\USdata.csv"
    Filename(4) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\ECdata.csv"
    Filename(5) = "C:\Users\drkle\Desktop\Desktop Transfer Material\Stock Market\TradeStation\Correlation data\ESdata.csv"
    
    match = 0
    nomatch = 0
    
    For i = 1 To 5:
        Workbooks.Open Filename(i)
        'Set Asset(i) = Filename(i)
        Set Asset(i) = aWB
    Next i
    
    Range("A1").CurrentRegion.Select
    nr = Selection.Rows.Count
    
    For j = 2 To nr:
        If Asset(1).Cells(j, 1) = Asset(2).Cells(j, 1) _
        And Asset(2).Cells(j, 1) = Asset(3).Cells(j, 1) _
        And Asset(3).Cells(j, 1) = Asset(4).Cells(j, 1) _
        And Asset(4).Cells(j, 1) = Asset(5).Cells(j, 1) Then
            match = match + 1
        Else
            nomatch = nomatch + 1
        End If
    Next j
    
    For k = 1 To 5:
        Asset(i).Close SaveChanges:=False
    Next k
    
    Range("A1").Value = match 'Doesn't matter whether .Value is at the end
    Range("A2") = nomatch
    
    Range("B1") = "dates matched across all five WBs"
    Range("B2") = "dates did not match across all five WBs"
    
    End Sub

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,982

    Re: How to name opened workbooks

    You only set aWb once, so it is the workbook that was active at that moment. It doesn't adjust as you open workbooks to always be whichever one is active. But you don't need it anyway - what you want, as shg said earlier, is:

    For i = 1 To 5:
         Set Asset(i) = Workbooks.Open(Filename(i))
    Next i
    Everyone who confuses correlation and causation ends up dead.

  11. #11
    Registered User
    Join Date
    08-25-2016
    Location
    USA
    MS-Off Ver
    14.0.7172.5000 (32-bit)
    Posts
    96

    Re: How to name opened workbooks

    Quote Originally Posted by rorya View Post
    You only set aWb once, so it is the workbook that was active at that moment. It doesn't adjust as you open workbooks to always be whichever one is active. But you don't need it anyway - what you want, as shg said earlier, is:

    For i = 1 To 5:
         Set Asset(i) = Workbooks.Open(Filename(i))
    Next i
    Got it.

    Continuing on, I still get an error here:
        If Asset(1).Cells(j, 1) = Asset(2).Cells(j, 1) _
        And Asset(2).Cells(j, 1) = Asset(3).Cells(j, 1) _
        And Asset(3).Cells(j, 1) = Asset(4).Cells(j, 1) _
        And Asset(4).Cells(j, 1) = Asset(5).Cells(j, 1) Then
    "Object doesn't support this property or method." I think I'm trying to use the Cells property on a workbook... is that a problem?

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,982

    Re: How to name opened workbooks

    Yes! A workbook doesn't have a Cells property. It has Worksheets, and each of them has a Cells property. If there's only one sheet in each workbook, you could use
    Asset(1).Activesheet.Cells(j, 1)
    for example.

  13. #13
    Registered User
    Join Date
    08-25-2016
    Location
    USA
    MS-Off Ver
    14.0.7172.5000 (32-bit)
    Posts
    96

    Re: How to name opened workbooks

    This portion works now. Thanks so much shg and rorya!

+ 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. Copy one sheet to all opened workbooks
    By ZoranC in forum Excel General
    Replies: 4
    Last Post: 01-09-2019, 08:34 AM
  2. Opened workbooks save as feature
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-10-2015, 05:45 PM
  3. Looping through opened workbooks
    By Hassan Seesodia in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-19-2013, 04:13 AM
  4. Counter for Opened workbooks
    By Johnr1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-21-2012, 11:53 AM
  5. How to make a macro available to any workbooks that may be opened.
    By Aran77 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-14-2008, 09:36 AM
  6. [SOLVED] always prompted to save opened workbooks
    By kjbrr in forum Excel General
    Replies: 4
    Last Post: 07-31-2006, 03:13 PM
  7. How to close forms opened from other workbooks...?
    By Andrew in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2005, 04: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