+ Reply to Thread
Results 1 to 3 of 3

Creating a Separate Logging Workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    07-09-2008
    Location
    Panama City, FL
    MS-Off Ver
    2007
    Posts
    42

    Creating a Separate Logging Workbook

    What I have is an Excel spreadsheet that allows users to access some information about an item by typing in its associated part number. What has been requested is an additional feature that is an "Add to List" button. This button would take the item currently being viewed and add it to a separate workbook, the idea being that as users browse through information, it would automatically record the parts that they want to remember in a separate file.

    In the sub for this button, what I am trying to do is to declare the target workbook to add the item to and assign it to a variable. Then, if an error occurs in that assignment, the workbook clearly does not exist and must be created. I thought this was the best way to create the file such that I didn't create a new one for each new item and such that I could reference it any time the button was called without having a global variable. I'm also perfectly open to other suggestions if this wasn't the best way to approach it.

    My code thus far is below. The problem is that, as expected, an error occurs when I try to set listbook. However, instead of using that error to get into the for loop, it just throws a "Subscript out of range" error and stops the program. Clearly I am not understanding how to appropriately use the IsError function, and any help in implementing this would be greatly appreciated. Thanks.

    ' Executes if the Add Item To List button is clicked
    Private Sub cmdAddToList_Click()
    
        ' Declare variables
        Dim listbook As Workbook
        Dim list As Worksheet
        Set listbook = Workbooks("List.xls")
        
        ' If this worksheet doesn't exist, create it
        If IsError(listbook) Then
        
            Workbooks.Add ("List.xls")
            Set listbook = Workbooks("List.xls")
            
        End If
        
    End Sub

  2. #2
    Registered User
    Join Date
    07-09-2008
    Location
    Panama City, FL
    MS-Off Ver
    2007
    Posts
    42
    I finally stumbled across a function online that supposedly checks to see if a file by some name is open.

    Function CheckFile(strFile As String) As Boolean
    
        Dim Wbk As Workbook
    
       CheckFile = False
       On Error GoTo NotOpen
       Set Wbk = Workbooks(strFile)
       CheckFile = True
       Exit Function
    
    NotOpen:
    
    End Function
    This seems to work, at least as far as I have been able to test it so far. However, I am running into problems when trying to create the file if it doesn't exist. Here's my code:

    ' If this worksheet doesn't exist, create it
        If Not (CheckFile("List.xls")) Then
            
            Workbooks.Add ("List.xls")
            Set listbook = Workbooks("List.xls")
                
            ' Create headings
            Set list = listbook.Sheets(1)
            list.Cells(1, 1).Value = "Part No."
            list.Cells(1, 2).Value = "Description"
            list.Cells(1, 3).Value = "Source Vendor"
            list.Cells(1, 4).Value = "Sales"
            list.Cells(1, 5).Value = "No. Orders"
            list.Cells(1, 6).Value = "B02 Status"
            list.Cells(1, 7).Value = "B02 Stock"
            list.Cells(1, 8).Value = "No. Competing Vendors"
            
        ' Otherwise, define the first sheet as the target
        Else
        
            Set listbook = Workbooks("List.xls")
            Set list = listbook.Sheets(1)
            
        End If
    However, I get an error on the Workbooks.Add("List.xls") line saying that the file counld not be found. I am confused - I thought this command was supposed to create a new file. Why then would I be getting this error, and how can I get around it?

  3. #3
    Registered User
    Join Date
    07-09-2008
    Location
    Panama City, FL
    MS-Off Ver
    2007
    Posts
    42
    Problem solved.

    Set listbook = Workbooks.Add
                With listbook
                    .SaveAs FileName:="List.xls"
                End With

+ 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