Consulting

Results 1 to 2 of 2

Thread: VBA Code to control Data Connectin from CSV File

  1. #1
    VBAX Newbie
    Joined
    Mar 2016
    Posts
    1
    Location

    VBA Code to control Data Connectin from CSV File

    Hi,

    I have a code that was kindly given to me to help me control a csv based data connection

    Sub UpdData()    Dim PCon As String
        PCon = "TEXT;" & Sheets("Data Connections").Range("B3").Value
        With Sheets("Pool").Range("A1").QueryTable
            .Connection = PCon
            .TextFileCommaDelimiter = True
            .Refresh BackgroundQuery:=False
        End With
        ActiveWorkbook.Connections("Pool").Refresh End Sub
    The Data goes onto sheet "Pool" based on the file source listed on "Data Connections" B3 but i cant seem to get the code working, it fails at
    With Sheets("Pool").Range("A1").QueryTable
    Any Help would be great, the Idea is that on open

    User Prompted by Input (results in B1)
    the result in B1 selects the required csv file path by vlookup
    the code generates the connection based on the file path. this would be repeated four times
    Pool in B3
    Schedule in B4
    Vehicle in B5
    KPI in B6

    I have attached the file for any input

    Thanks

    Danny
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    That's because there is no querytable there!
    Connect manually once first selecting cell A1 of the Pool tab then using the Data tab of the ribbon, from Text etc. and make sure it's called "Pool" (if there isn't already another connection of that name, or comment-out the line: ActiveWorkbook.Connections("Pool").Refresh).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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