Consulting

Results 1 to 9 of 9

Thread: Filename Variable

  1. #1
    VBAX Regular
    Joined
    Feb 2017
    Posts
    18
    Location

    Filename Variable

    I wanted to see if this is at all possible with the way I'm importing files into my excel sheet.

    I've got VBA code that allows me to import the file from the relative directory.

    VBA Code I'm using:

    MakeLink ThisWorkbook.Path & "\" & "msg_adv.csv", "MessageSize", "MessageSize!A1", 1, Array(5), "m/d/yyyy"

    File name could look like:

    • msg_adv.csv
    • customer_msg_adv.csv
    • customermsg_adv.csv


    My question is that sometimes the file I want to import has a prefix. Is there a way to script for the possibility of a prefix?

    Thanks!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could run a dir command to test for any prefix and pass the result to your code
    x = Dir(ThisWorkbook.Path & "\*msg_adv.csv")
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Feb 2017
    Posts
    18
    Location
    So I'm a bit of a VBA noob so I was just hoping I could simply modify my import command to include:
    MakeLink ThisWorkbook.Path & "\" & "\*disp_cluster_overview.csv", "test", "test!A1", 1, Array(2), "@"

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you have more than one variation in the folder then the first function can be modified to loop through all results.
    If you will have only one then either should work.

    x = Dir(ThisWorkbook.Path & "\*msg_adv.csv")
    MakeLink ThisWorkbook.Path & "\" & x, "MessageSize", "MessageSize!A1", 1, Array(5), "m/d/yyyy"
    'or
    MakeLink ThisWorkbook.Path & "\" & Dir(ThisWorkbook.Path & "\*msg_adv.csv"), "MessageSize", "MessageSize!A1", 1, Array(5), "m/d/yyyy"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Feb 2017
    Posts
    18
    Location
    Many thanks... the first section works like a charm.

  6. #6
    VBAX Regular
    Joined
    Feb 2017
    Posts
    18
    Location
    Actually it only works on the tab that is currently selected. Overwrote data on another sheet.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Without MakeLink code we can't advise.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Regular
    Joined
    Feb 2017
    Posts
    18
    Location
    Private Sub Workbook_Open()    CSVImport
    End Sub
    Sub CSVImport()
        On Error Resume Next
        For Each c In ThisWorkbook.Connections
            c.Delete
        Next
    	
        MakeLink ThisWorkbook.Path & "\" & "top_sites.csv", "TopSites", "TopSites!D1", 1, Array(2), "@"
        MakeLink ThisWorkbook.Path & "\" & "sites_by_months.csv", "SitesMonths", "SitesMonths!H17", 1, Array(1, 1, 9), "m/d/yyyy"
    	x = Dir(ThisWorkbook.Path & "\*msg_adv.csv") 
    	MakeLink ThisWorkbook.Path & "\" & x, "Test", "Test!A1", 1, Array(5), "m/d/yyyy" 
    	
    	End Sub
    
    
    	Sub MakeLink(strFileName As String, strSheetName As String, strRangeAddress As String, startRow As Long, FirstColmcsvFormat, FirstColmSheetFormat)
        With Sheets(strSheetName).QueryTables.Add(Connection:="TEXT;" & strFileName, Destination:=Range(strRangeAddress))
             '.Name = "ABC" 'no need to name every query the same (unless you're going to use them)!
             '.FieldNames = True'default
             '.RowNumbers = False 'default
             '.FillAdjacentFormulas = False 'default
             '.PreserveFormatting = True 'default
             '.RefreshOnFileOpen = False 'default
            .RefreshStyle = xlOverwriteCells
             '.SavePassword = False 'default
             '.SaveData = True 'default
             '.AdjustColumnWidth = True 'default
             '.RefreshPeriod = 0 'default
             '.TextFilePromptOnRefresh = False 'default
            .TextFilePlatform = 437
            .TextFileStartRow = startRow
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
             '.TextFileColumnDataTypes = Array(2)
             '.TextFileColumnDataTypes = Array(5)
            .TextFileColumnDataTypes = FirstColmcsvFormat
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
            .ResultRange.Columns(1).NumberFormat = FirstColmSheetFormat
             '.ResultRange.Columns(1).NumberFormat = "m/d/yyyy" 'or however you want it.
        End With
    End Sub

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This looks intended to import data from 3 csv files to 3 worksheets when the workbook is opened. What is the issue?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •