简体   繁体   English

QueryTables.Add(Connection) 错误,应为“=”

[英]QueryTables.Add(Connection) ERROR , Expected '='

So, first of all, i am not familiar with VBA.所以,首先,我不熟悉 VBA。 I've been trying to learn VBA because at my job that's what drives the backbone of our database and our excel workflow (although no one at my division seems to know how any of it works).我一直在努力学习 VBA,因为在我的工作中,这是驱动我们数据库和 excel 工作流程的支柱(尽管我所在部门似乎没有人知道它是如何工作的)。 And i've been trying to write a macro that could automate some of the work i have to do, namely: Importing CSV files into an excel to process them.而且我一直在尝试编写一个宏,它可以自动化我必须做的一些工作,即:将 CSV 文件导入 excel 以处理它们。 I've been trying to approach this problem step by step, by dividing into smaller subsets of problems.我一直试图通过将问题分成更小的子集来逐步解决这个问题。

So I have been able to add a query into my excel, that allows it to look into a folder to see which files are in there.所以我已经能够在我的 excel 中添加一个查询,它允许它查看一个文件夹以查看其中有哪些文件。 This allows the macro to 'see' different file paths and filenames.这允许宏“查看”不同的文件路径和文件名。

What i am trying to accomplish now is to have the macro loop through all the files it can see in the folder, and import them to a sheet called 'output'.我现在想要完成的是让宏循环遍历它在文件夹中可以看到的所有文件,并将它们导入到名为“输出”的工作表中。

No matter what i've tried, and how much research i did, i can't figure out the import macro.无论我尝试过什么,也无论我做了多少研究,我都无法弄清楚导入宏。 It keeps giving me a compile error that it expects '='它不断给我一个编译错误,它期望 '='

(I know that the macro is able to read the filenames etc correctly, because i have created a sub that makes it 'log' whatever it reads, to test its ability to differentiate between file types) (我知道宏能够正确读取文件名等,因为我创建了一个子程序,使其“记录”读取的内容,以测试其区分文件类型的能力)

Dim ImportFolder As String
Dim ImportRow As Range
Dim ImportFilename As Range
Dim ImportFilenameS As String
Dim ImportAccessDate As Range
Dim ImportFilePath As Range
Dim ImportExtension As Range

Dim ImportRange As Range
Dim ImportVar As Integer
Dim ImportLength As Integer
Dim L

Dim LogRow As Range
Dim LogFilename As Range
Dim LogAccessDate As Range
Dim LogFilePath As Range
Dim LogStatus As Range
Dim LogReason As Range

Dim OutputFolder As String
Dim OutputRange As Range

Sub FileTypeController()

Set ImputFolder = "H:\BLM.Workflow\CSV.Workflow-Input\"

Set ImportExtension = ThisWorkbook.Worksheets("Import").Range("B2")
Set ImportFilename = ThisWorkbook.Worksheets("Import").Range("A2")
Set ImportAccessDate = ThisWorkbook.Worksheets("Import").Range("C2")
Set ImportFilePath = ThisWorkbook.Worksheets("Import").Range("G2")

Set LogFilename = ThisWorkbook.Worksheets("Log").Range("A2")
Set LogAccessDate = ThisWorkbook.Worksheets("Log").Range("B2")
Set LogFilePath = ThisWorkbook.Worksheets("Log").Range("C2")

Set LogStatus = ThisWorkbook.Worksheets("Log").Range("D2")
Set LogReason = ThisWorkbook.Worksheets("Log").Range("E2")

Set ImportRange = ThisWorkbook.Worksheets("Import").Range("A:A")
    ImportVar = WorksheetFunction.CountA(ImportRange)
    ImportLength = (ImportVar - 1)

For L = 1 To ImportLength

    If ImportExtension.Value = ".csv" Or ImportExtension.Value = ".txt" Then 
Call CSVToOutput Else Call ExtensionTypeFailedImport

Set ImportExtension = ImportExtension.Offset(1, 0)

Set ImportFilename = ImportFilename.Offset(1, 0)
Set ImportAccessDate = ImportAccessDate.Offset(1, 0)
Set ImportFilePath = ImportFilePath.Offset(1, 0)

Set LogFilename = LogFilename.Offset(1, 0)
Set LogAccessDate = LogAccessDate.Offset(1, 0)
Set LogFilePath = LogFilePath.Offset(1, 0)


Set LogStatus = LogStatus.Offset(1, 0)
Set LogReason = LogReason.Offset(1, 0)


Next

ThisWorkbook.Worksheets("Log").Activate

End Sub

Sub CSVToOutput()

ImportFilenameS = "TEXT;" & ImportFilename.Value
Set OutputRange = ThisWorkbook.Worksheets("Output").Range("A1")

'Application.CutCopyMode = False
With ThisWorkbook.Worksheets("Output").QueryTables_
    .Add(Connection:= ImportFilenameS,Destination:= OutputRange)
    .Name = "Importfilename"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 1252
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False

Call SuccessfulImport
End Sub

It's still a first draft, so i haven't taken the time to clean it up a little yet.它仍然是初稿,所以我还没有花时间清理它。 Apologies if the code is trash, but, i'm giving it my all ;-)如果代码是垃圾,请道歉,但是,我全力以赴;-)

At first I spot an error here:起初我在这里发现了一个错误:

Set ImputFolder = "H:\BLM.Workflow\CSV.Workflow-Input\"

Reason: you only use the reserved word Set with objects, and in this case, you are just assigning a value to a string variable.原因:您只对对象使用保留字Set ,在这种情况下,您只是为字符串变量赋值。

Also, I didn't see that variable defined anywhere.另外,我没有看到在任何地方定义的变量。 Maybe you meant to use this one:也许你打算使用这个:

Dim ImportFolder As String

Those facts aside, I suggest that you look into Power Query .撇开这些事实不谈,我建议您查看Power Query If you have Excel 2010, 2013 you can download it as an add-in or if you have 2016-2019 is already available in the Ribbon's Data tab.如果您有 Excel 2010、2013,您可以将其作为加载项下载,或者如果您有 2016-2019 已在功能区的数据选项卡中可用。

Check Matt's tutorial on how to combine files from a folder here查看 Matt 的教程,了解如何在此处组合文件夹中的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM