+ Reply to Thread
Results 1 to 6 of 6

InputBox to enter file

Hybrid View

  1. #1
    Registered User
    Join Date
    07-17-2008
    Location
    New Hamphire
    Posts
    4

    InputBox to enter file

    Hello,
    I am trying to program a macro that I can enter a file name in an input box and have that file be imported into a specific spreadsheet. This is the code I have so far. This macro was created using the macro tool in excel. I tried to add the Sub GetInput() but when I take away the ' marks I get an expected end sub error.

    'Sub GetInput()
           ' Dim MyInputDir As String       'This line of code is optional
           ' Dim MyInputFile As String
           ' MyInputDir = InputBox("Enter the Directory Name")
           ' MyInputFile = MyInputDir & "_spurscan.csv"
           'End Sub
    
    Sub ImportSensitivityData()
    '
    ' ImportSensitivityData Macro
    ' Macro recorded 7/16/2008 by tom.donahue
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\PBK1350ReceiverData\SN052375\SN052375_sensitivity.csv", Destination _
            :=Cells)
            .Name = "SN052375_sensitivity_1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(2, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    End Sub
    Last edited by davesexcel; 07-17-2008 at 07:57 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486
    Hi,
    Doesn't the folder require a backslash?
    Please wrap your code,

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Your code looks ok to me

    Sub GetInput()
       Dim MyInputDir As String       'This line of code is optional
       Dim MyInputFile As String
       MyInputDir = InputBox("Enter the Directory Name")
       MyInputFile = MyInputDir & "_spurscan.csv"
    End Sub
    Using an Input box can lead to errors due to mis-typing, spelling mistakes etv

    This code will allow the user to select an actual folder
    Private Type BrowseInfo ' used by the function GetFolderName
        hOwner As Long
        pidlRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As Long
        lParam As Long
        iImage As Long
    End Type
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
        Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
        Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
    
    
    Sub TestGetFolderName()
    Dim FolderName As String
        FolderName = GetFolderName("Select a folder")
        If FolderName = "" Then
            MsgBox "You didn't select a folder."
        Else
            MsgBox "You selected this folder: " & FolderName
        End If
    End Sub
    
    Function GetFolderName(Msg As String) As String
    ' returns the name of the folder selected by the user
    Dim bInfo As BrowseInfo, Path As String, r As Long
    Dim X As Long, pos As Integer
        bInfo.pidlRoot = 0& ' Root folder = Desktop
        If IsMissing(Msg) Then
            bInfo.lpszTitle = "Select a folder."
            ' the dialog title
        Else
            bInfo.lpszTitle = Msg ' the dialog title
        End If
        bInfo.ulFlags = &H1 ' Type of directory to return
        X = SHBrowseForFolder(bInfo) ' display the dialog
        ' Parse the result
        Path = Space$(512)
        r = SHGetPathFromIDList(ByVal X, ByVal Path)
        If r Then
            pos = InStr(Path, Chr(0))
            GetFolderName = Left(Path, pos - 1)
        Else
            GetFolderName = ""
        End If
    End Function
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Registered User
    Join Date
    07-17-2008
    Location
    New Hamphire
    Posts
    4

    I added the suggested code and now I get a error message

    Quote Originally Posted by mudraker
    Your code looks ok to me

    [CODE]
    Private Type BrowseInfo ' used by the function GetFolderName
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
    End Type


    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
    Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
    Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long

    Function GetFolderName(Msg As String) As String




    ' returns the name of the folder selected by the user



    Dim bInfo As BrowseInfo, Path As String, r As Long
    Dim X As Long, pos As Integer
    bInfo.pidlRoot = 0& ' Root folder = Desktop
    If IsMissing(Msg) Then
    bInfo.lpszTitle = "Select a folder."
    ' the dialog title
    Else
    bInfo.lpszTitle = Msg ' the dialog title
    End If
    bInfo.ulFlags = &H1 ' Type of directory to return
    X = SHBrowseForFolder(bInfo) ' display the dialog
    ' Parse the result
    Path = Space$(512)
    r = SHGetPathFromIDList(ByVal X, ByVal Path)
    If r Then
    pos = InStr(Path, Chr(0))
    GetFolderName = Left(Path, pos - 1)
    Else
    GetFolderName = ""
    End If

    End Function


    Sub TestGetFolderName()
    Dim FolderName As String
    FolderName = GetFolderName("Select a folder")
    If FolderName = "" Then
    MsgBox "You didn't select a folder."
    Else
    MsgBox "You selected this folder: " & FolderName
    End If
    End Sub

    Sub ImportSensitivityData()

    'With ActiveSheet.QueryTables.Add(Connection:= _
    '"TEXT;C:\PBK1350ReceiverData\SN052375\SN052375_sensitivity.csv", Destination _
    ':=Cells)
    '.Name = "SN052375_sensitivity_1"


    With ActiveSheet.QueryTables



    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = True
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(2, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub

    Using an Input box can lead to errors due to mis-typing, spelling mistakes etv

    This code will allow the user to select an actual folder
    Private Type BrowseInfo ' used by the function GetFolderName
        hOwner As Long
        pidlRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As Long
        lParam As Long
        iImage As Long
    End Type
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
        Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
        Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
    
    
    Sub TestGetFolderName()
    Dim FolderName As String
        FolderName = GetFolderName("Select a folder")
        If FolderName = "" Then
            MsgBox "You didn't select a folder."
        Else
            MsgBox "You selected this folder: " & FolderName
        End If
    End Sub
    
    Function GetFolderName(Msg As String) As String
    ' returns the name of the folder selected by the user
    Dim bInfo As BrowseInfo, Path As String, r As Long
    Dim X As Long, pos As Integer
        bInfo.pidlRoot = 0& ' Root folder = Desktop
        If IsMissing(Msg) Then
            bInfo.lpszTitle = "Select a folder."
            ' the dialog title
        Else
            bInfo.lpszTitle = Msg ' the dialog title
        End If
        bInfo.ulFlags = &H1 ' Type of directory to return
        X = SHBrowseForFolder(bInfo) ' display the dialog
        ' Parse the result
        Path = Space$(512)
        r = SHGetPathFromIDList(ByVal X, ByVal Path)
        If r Then
            pos = InStr(Path, Chr(0))
            GetFolderName = Left(Path, pos - 1)
        Else
            GetFolderName = ""
        End If
    End Function

    Last edited by excelvba; 07-17-2008 at 09:31 AM. Reason: I moved some code around and got rid of the error message. Now I can get to a folder but not the file I need and the importing function does not work.

  5. #5
    Registered User
    Join Date
    07-17-2008
    Location
    New Hamphire
    Posts
    4

    Got rid of error still not working

    Quote Originally Posted by excelvba
    Got rid of the error by changing the sequence of some of the code. I can only get to the folder level. I need to get the File level and the importing function does not work. Any help would beappreciated.


    
    Private Type BrowseInfo ' used by the function GetFolderName
        hOwner As Long
        pidlRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As Long
        lParam As Long
        iImage As Long
    End Type
    
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
            Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
            Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
            Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
            
    Function GetFolderName(Msg As String) As String
    
    
            
            
    ' returns the name of the folder selected by the user
    
    
    
    Dim bInfo As BrowseInfo, Path As String, r As Long
    Dim X As Long, pos As Integer
        bInfo.pidlRoot = 0& ' Root folder = Desktop
        If IsMissing(Msg) Then
            bInfo.lpszTitle = "Select a folder."
            ' the dialog title
        Else
            bInfo.lpszTitle = Msg ' the dialog title
        End If
        bInfo.ulFlags = &H1 ' Type of directory to return
        X = SHBrowseForFolder(bInfo) ' display the dialog
        ' Parse the result
        Path = Space$(512)
        r = SHGetPathFromIDList(ByVal X, ByVal Path)
        If r Then
            pos = InStr(Path, Chr(0))
            GetFolderName = Left(Path, pos - 1)
        Else
            GetFolderName = ""
        End If
        
    End Function
    
    
    Sub TestGetFolderName()
    Dim FolderName As String
        FolderName = GetFolderName("Select a folder")
        If FolderName = "" Then
            MsgBox "You didn't select a folder."
        Else
            MsgBox "You selected this folder: " & FolderName
        End If
    End Sub
    
    Sub ImportSensitivityData()
    
     'With ActiveSheet.QueryTables.Add(Connection:= _
            '"TEXT;C:\PBK1350ReceiverData\SN052375\SN052375_sensitivity.csv", Destination _
            ':=Cells)
            '.Name = "SN052375_sensitivity_1"
    
    
              With ActiveSheet.QueryTables
           
            
            
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(2, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    End Sub
    [/QUOTE]

  6. #6
    Registered User
    Join Date
    07-17-2008
    Location
    New Hamphire
    Posts
    4

    Help found

    Looking through my Excel VBA book I found an example that solved my problem.

+ 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