Results 1 to 2 of 2

VBA to open an external excel file and direct to the specific spreadsheet

Threaded View

  1. #1
    Registered User
    Join Date
    12-22-2011
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    1

    Question VBA to open an external excel file and direct to the specific spreadsheet

    Dear all,

    This is my first post and need your kind help. I am totally new in VBA.

    Here is the case:

    I want the user to input 3 things : workbook name, spreadsheet name, password. Then Click on an active button, the required workbook will open, and display the required spreadsheet.

    Below is the code i have, but it can only open the specified workbook, not able to direct to the specified spreadsheet. Please help. Thank you all.

    Private Sub CommandButton2_Click()
    
    Dim varFNamePath As Variant
    Dim colFoundFiles As New Collection
    Dim strFilter As String
    Dim strPath As String
    Dim myRow As Long
    
    strPath = "C:\Documents and Settings\zaizhi zhang\Desktop\test"
    
    strFilter = "*" & Sheets("menu").Range("E17").Value & "*.*"
    
    Call FileSearch(colFoundFiles, strPath, strFilter, True)
    
    If colFoundFiles.Count = 0 Then
        MsgBox "No file was found!"
        Exit Sub
    
    End If
    
    ActiveWorkbook.FollowHyperlink Address:=colFoundFiles(1)
    Application.CommandBars("Web").Visible = False
    
    End Sub
    
    
    Private Sub FileSearch(colFoundFiles As Collection, strPath As String, _
        strMask As String, blnIncSubDir As Boolean)
    
    Dim strDirFile As String
    Dim varColItem As Variant
    Dim colSubDir As New Collection
    
    strPath = Trim(strPath)
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    
    strDirFile = Dir(strPath & strMask)
    Do While strDirFile <> ""
        colFoundFiles.Add strPath & strDirFile
        strDirFile = Dir
    Loop
    
    If Not blnIncSubDir Then Exit Sub
    
    strDirFile = Dir(strPath & "*", vbDirectory)
    Do While strDirFile <> ""
        If strDirFile <> "." And strDirFile <> ".." Then If ((GetAttr(strPath & strDirFile) And _
            vbDirectory) = 16) Then colSubDir.Add strPath & strDirFile
        strDirFile = Dir
    Loop
    
    For Each varColItem In colSubDir
         Call FileSearch(colFoundFiles, CStr(varColItem), strMask, blnIncSubDir)
    Next
    
    
    End Sub
    Last edited by pike; 12-22-2011 at 06:03 AM. Reason: add code tags for newbie PM rules

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