QueryTables.Add

30percent

Board Regular
Joined
May 5, 2011
Messages
118
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have the following procedure. Instead of hardcoding the path name, I wanted to assign it to a variable as pass it to the method QueryTables.Add. However, I got the following error message. compile error expected: list separator or )


Code:
Sub import_Data(ByVal fso As Object, ByVal file As Object, ByVal minus_day As Integer, ByVal basePath)




Dim pathName As String


thisYear = Year(Date)
month_Name = Left(monthName(Month(Date)), 3)




pathName = basePath & "\" & thisYear & "\" & month_Name & "\" & get_FolderDate(minus_day) & "\" & fso.getbasename(file.Name) & ".csv"


'With Worksheets("Today").QueryTables.Add(Connection:="TEXT;C:\Users\2017\Nov\11-13-2017\Order-11-13-2017.csv", _
'Destination:=Range("Today!$A$1"))
'When I hardcode the pathname as the comment above, it works but get error message when I pass in a variable.


With Worksheets("Today").QueryTables.Add(Connection:="TEXT; pathName, _
Destination:=Range("Today!$A$1"))




    .FieldNames = True
    .TextFileParseType = xlDelimited
    .TextFileCommaDelimiter = True
    .Refresh
End With


End Sub

helper function:

Code:
Public Function get_FolderDate(ByVal minus_day As Integer)


Dim folder_Date As String
Dim thisYear As String
Dim sessionDay As String


thisYear = Year(Date)
sessionDay = CStr(Day(Date) - minus_day)
folder_Date = Month(Date) & "-" & sessionDay & "-" & thisYear


get_FolderDate = folder_Date


End Function

Intend to call the procedure as follows:

Code:
Sub call_import_Data()


Dim basePath as String
Dim fso as Object


basePath = "C:\Users"
Set fso = CreateObject("Scripting.FileSystemObject")


For Each file In fso.GetFolder(basePath).Files


    If UCase(fso.getextensionName(file.Name)) = "CSV" Then
        import_Data fso, file, 1, basePath
    End If


End If




End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
It looks like you're missing a quote ("") and an ampersand (&)...

Code:
With Worksheets("Today").QueryTables.Add(Connection:="TEXT;[COLOR=#ff0000]" & [/COLOR]pathName, _
     Destination:=Range("Today!$A$1"))
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,981
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top