+ Reply to Thread
Results 1 to 6 of 6

Import dynamic filname

Hybrid View

  1. #1
    Registered User
    Join Date
    04-19-2013
    Location
    midwest usa
    MS-Off Ver
    2007, 2010
    Posts
    16

    Import dynamic filname

    I'm importing some CSVs that have names that change every month (oct13, nov13, dec13, etc.)

    I would like to create a variable that contains a wildcard, but VBA doesn't seem to like what I've written.

    If I write the whole file name in, it works correctly, but if I add in an *, it doesn't
    • This variable works --> strFileName = "Oct13_Future" & ".csv"
    • This is what I'm trying to accomplish --> strFileName = "*" & "_Future" & ".csv"

    Any assistance would be greatly appreciated.

    Thanks,
    7

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Import dynamic filname

    Perhaps a construction along these lines if you a checking a number of files in a folder?

    Dim FileName As String
    FileName = Dir("C:\tester\*.csv")
    Do While Len(FileName) > 0
       If Right(FileName, 11) = "_Future.csv" Then
    Your code goes here
       End If
        FileName = Dir
    Loop
    Alf
    Last edited by Alf; 11-01-2013 at 04:55 AM. Reason: fixed what I did wrong before.

  3. #3
    Registered User
    Join Date
    04-19-2013
    Location
    midwest usa
    MS-Off Ver
    2007, 2010
    Posts
    16

    Re: Import dynamic filname

    not sure how to work that into my script, as I think I need the full filename to build my connection.

    here's what I've got so far, and it works, but since the filenames will change each month, I need the strConnection variable to be dynamic.

    again; any help will be greatly appreciated

    Sub ProcessDTA()
    
        Dim wbPath, strFutureName, strHistoryName, strConnection, strTempFile As String
    
        wbPath = Application.ActiveWorkbook.Path
        strFutureName = "ameroct13_Future" & ".csv"
        strHistoryName = "ameroct13_History" & ".csv"
    
        Dim row As Integer
        row = 2
        On Error Resume Next
        row = Range("A1").End(xlDown).row + 1
    
        strConnection = "TEXT;" & wbPath & "\Data\" & strFutureName
        With ActiveSheet.QueryTables.Add(Connection:= _
             strConnection, Destination:=Range("$A$" & row))
            .Name = "temp"
            .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 = 2
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    
        Workbooks.Open (wbPath & "\Data\" & strHistoryName)
        wbPath = Application.ActiveWorkbook.Path
            
        Columns("AG:AK").Select
        Selection.Cut
        Columns("Y:Y").Select
        Selection.Insert Shift:=xlToRight
        Columns("Y:AX").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        ActiveWorkbook.SaveAs Filename:=wbPath & "\tmp_" & strHistoryName, FileFormat:=xlCSV, CreateBackup:=False
            
        Application.DisplayAlerts = False
            ActiveWorkbook.Close
        Application.DisplayAlerts = True
    
        row = Range("A1").End(xlDown).row + 1
        strConnection = "TEXT;" & wbPath & "\tmp_" & strHistoryName
        With ActiveSheet.QueryTables.Add(Connection:= _
             strConnection, Destination:=Range("$A$" & row))
            .Name = "temp"
            .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 = 2
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    
        strTempFile = wbPath & "\tmp_" & strHistoryName
        Kill strTempFile
    
        MsgBox "Data Transformation Complete", vbInformation, "Data Transformation"
    
    End Sub

  4. #4
    Registered User
    Join Date
    04-19-2013
    Location
    midwest usa
    MS-Off Ver
    2007, 2010
    Posts
    16

    Re: Import dynamic filname

    edit: double post
    Last edited by Sevn; 11-01-2013 at 10:34 AM. Reason: duplicate

  5. #5
    Registered User
    Join Date
    04-19-2013
    Location
    midwest usa
    MS-Off Ver
    2007, 2010
    Posts
    16

    Re: Import dynamic filname

    ok, it's a little rough around the edges, but I got it figured out. Thanks for your help.

    Sub ProcessDTA()
    
    On Error GoTo ExitPoint
    
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
        Dim wbPath, MyDocuments, strToday, strFileName, strFutureName, strHistoryName, strConnection, strTempFile As String
    
        wbPath = Application.ActiveWorkbook.Path
        'MyDocuments = Environ$("USERPROFILE") & "\My Documents"
        'strToday = Format(Date, "mmddyy")
        strFileName = Dir(wbPath & "\Data\*.csv")
    
        Do While Len(strFileName) > 0
            If Right(strFileName, 11) = "_Future.csv" Then
                strFutureName = strFileName
                
                Sheets("Data").Select
                
                Dim row As Integer
                row = 2
                On Error Resume Next
                row = Range("A1").End(xlDown).row + 1
    
                strConnection = "TEXT;" & wbPath & "\Data\" & strFutureName
                With ActiveSheet.QueryTables.Add(Connection:= _
                    strConnection, Destination:=Range("$A$" & row))
                    .Name = "temp"
                    .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 = 2
                    .TextFileParseType = xlDelimited
                    .TextFileTextQualifier = xlTextQualifierDoubleQuote
                    .TextFileConsecutiveDelimiter = False
                    .TextFileTabDelimiter = False
                    .TextFileSemicolonDelimiter = False
                    .TextFileCommaDelimiter = True
                    .TextFileSpaceDelimiter = False
                    .TextFileColumnDataTypes = Array(1)
                    .TextFileTrailingMinusNumbers = True
                    .Refresh BackgroundQuery:=False
                End With
            
            ElseIf Right(strFileName, 12) = "_History.csv" Then
                strHistoryName = strFileName
    
                Workbooks.Open (wbPath & "\Data\" & strHistoryName)
                wbPath = Application.ActiveWorkbook.Path
            
                    Columns("AG:AK").Select
                    Selection.Cut
                    Columns("Y:Y").Select
                    Selection.Insert Shift:=xlToRight
                    Columns("Y:AX").Select
                    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    ActiveWorkbook.SaveAs Filename:=wbPath & "\tmp_" & strHistoryName, FileFormat:=xlCSV, CreateBackup:=False
            
                    Application.DisplayAlerts = False
                        ActiveWorkbook.Close
                    Application.DisplayAlerts = True
    
                    row = Range("A1").End(xlDown).row + 1
    
                    strConnection = "TEXT;" & wbPath & "\tmp_" & strHistoryName
                    With ActiveSheet.QueryTables.Add(Connection:= _
                        strConnection, Destination:=Range("$A$" & row))
                        .Name = "temp"
                        .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 = 2
                        .TextFileParseType = xlDelimited
                        .TextFileTextQualifier = xlTextQualifierDoubleQuote
                        .TextFileConsecutiveDelimiter = False
                        .TextFileTabDelimiter = False
                        .TextFileSemicolonDelimiter = False
                        .TextFileCommaDelimiter = True
                        .TextFileSpaceDelimiter = False
                        .TextFileColumnDataTypes = Array(1)
                        .TextFileTrailingMinusNumbers = True
                        .Refresh BackgroundQuery:=False
                    End With
    
                    strTempFile = wbPath & "\tmp_" & strHistoryName
                    Kill strTempFile
            End If
            
            strFileName = Dir
        Loop
    
        Sheets("Home").Select
    
        MsgBox "Data Transformation Complete", vbInformation, "Data Transformation"
    
    ExitPoint:
    With Application
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    End Sub

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Import dynamic filname

    ok, it's a little rough around the edges, but I got it figured out.
    Perhaps it is but it works and that is the main thing! Thanks for feedback and rep.

    Alf

    Ps As this seems to fix your problem don't forget to mark thread "Solved"

    To mark your thread solved do the following:
    New Method
    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved
    Last edited by Alf; 11-02-2013 at 05:08 AM.

+ 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] Power Pivot SQL Data Import - How to update import filter
    By minnesotaart in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 10-29-2013, 12:39 PM
  2. Implementing TextFile import Delimiter settings on TXT Import VBA Script
    By Canuckle777 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-19-2013, 02:06 PM
  3. Macro that change filname i series if one already exists
    By Gamla kläder in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2013, 02:52 AM
  4. Dynamic Import of External Data Depending on Date
    By jponte in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2009, 01:43 PM
  5. Dynamic Field Name when Import into Access
    By pickledmuffin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2008, 10:13 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