记录宏:从文件获取数据

时间:2019-01-03 10:39:25

标签: excel vba get record

我首先要承认宏不是我的事,编写代码当然不是我的事,但是在这种情况下,并且阅读了有关录制宏的内容后,我认为它可能对我有用。如果这不是做到这一点的方法,那么可以这样说,在我可以进行更多研究和编写代码之前,我暂时不考虑这个想法。

基本上是我要实现的步骤 -从空白工作簿开始 -使用宏从我们公司网络中包含大约15个工作簿的文件夹中获取数据,这些工作簿都具有相同的内部格式和信息,但涉及不同的地理位置 -如果我手动执行任务,我将按照正确的步骤确切地记录宏,

完整的代码文本如下。运行后,我立即收到一个运行时错误,在调试时突出显示了第一部分,并指出“名称为'Pipeline Working'的查询已经存在。“ Pipeline Working”是包含要导入的工作簿的文件夹的名称,从那里我已经迷路了。

任何帮助表示赞赏。

Sub PipelinePredictions()
'
' PipelinePredictions Macro
' Import Data from File for Pipeline Predictions
'

'
    ActiveWorkbook.Queries.Add Name:="Pipeline Working", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Folder.Files(""\\hofileserver\Accounts\ACCOUNTS\Lettings Stats 2018 Qtr 4\Pipelines\Pipeline Working"")," & Chr(13) & "" & Chr(10) & "    #""Filtered Hidden Files1"" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true)," & Chr(13) & "" & Chr(10) & "    #""Invoke Custom Function1"" = Table.AddColumn(#""Filtered Hidden Files1"", ""Transform File from Pipeline Working"", each #""Transform File f" & _
        "rom Pipeline Working""([Content]))," & Chr(13) & "" & Chr(10) & "    #""Renamed Columns1"" = Table.RenameColumns(#""Invoke Custom Function1"", {""Name"", ""Source.Name""})," & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns1"" = Table.SelectColumns(#""Renamed Columns1"", {""Source.Name"", ""Transform File from Pipeline Working""})," & Chr(13) & "" & Chr(10) & "    #""Expanded Table Column1"" = Table.ExpandTableColumn(#""Removed Other Columns" & _
        "1"", ""Transform File from Pipeline Working"", Table.ColumnNames(#""Transform File from Pipeline Working""(#""Sample File"")))," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Expanded Table Column1"",{{""Source.Name"", type text}, {""Column1"", type any}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type number}" & _
        ", {""Column6"", Percentage.Type}, {""Column7"", Int64.Type}, {""Column8"", type number}, {""Column9"", type number}, {""Column10"", type number}, {""Column11"", type any}, {""Column12"", type datetime}, {""Column13"", type text}})," & Chr(13) & "" & Chr(10) & "    #""Renamed Columns"" = Table.RenameColumns(#""Changed Type"",{{""Source.Name"", ""Branch""}, {""Column1"", ""Week Booked""}, {""Col" & _
        "umn2"", ""Neg""}, {""Column3"", ""Property""}, {""Column4"", ""Service Type""}, {""Column5"", ""Rent""}, {""Column6"", ""Percentage Fee""}, {""Column7"", ""Term""}, {""Column8"", ""Fee""}, {""Column9"", ""Admin""}, {""Column10"", ""Total Fees""}, {""Column11"", ""Rent G'tee""}})," & Chr(13) & "" & Chr(10) & "    #""Changed Type1"" = Table.TransformColumnTypes(#""Renamed Columns"",{{""Column12" & _
        """, type date}})," & Chr(13) & "" & Chr(10) & "    #""Renamed Columns2"" = Table.RenameColumns(#""Changed Type1"",{{""Column12"", ""Move in Date""}, {""Column13"", ""Notes""}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Renamed Columns2"""
    ActiveWorkbook.Queries.Add Name:="Sample File Parameter1", Formula:= _
        "#""Sample File"" meta [IsParameterQuery=true, BinaryIdentifier=#""Sample File"", Type=""Binary"", IsParameterQueryRequired=true]"
    ActiveWorkbook.Queries.Add Name:= _
        "Transform Sample File from Pipeline Working", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(#""Sample File Parameter1"", null, true)," & Chr(13) & "" & Chr(10) & "    #""'A Pipeline$'Print_Area1"" = Source{[Name=""'A Pipeline$'Print_Area""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""'A Pipeline$'Print_Area1"""
    ActiveWorkbook.Queries.Add Name:="Sample File", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Folder.Files(""\\hofileserver\Accounts\ACCOUNTS\Lettings Stats 2018 Qtr 4\Pipelines\Pipeline Working"")," & Chr(13) & "" & Chr(10) & "    Navigation1 = Source{0}[Content]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Navigation1"
    ActiveWorkbook.Queries.Add Name:="Transform File from Pipeline Working", _
        Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = (#""Sample File Parameter1"") => let" & Chr(13) & "" & Chr(10) & "        Source = Excel.Workbook(#""Sample File Parameter1"", null, true)," & Chr(13) & "" & Chr(10) & "        #""'A Pipeline$'Print_Area1"" = Source{[Name=""'A Pipeline$'Print_Area""]}[Data]" & Chr(13) & "" & Chr(10) & "    in" & Chr(13) & "" & Chr(10) & "        #""'A Pipeline$'Print_Area1""" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Pipeline Working"";Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [Pipeline Working]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_ExternalData_1"
        .Refresh BackgroundQuery:=False
    End With
    Workbooks("Book1").Connections.Add2 "Query - Sample File Parameter11", _
        "Connection to the 'Sample File Parameter1' query in the workbook.", _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File Parameter1"";Extended Properties=""""" _
        , "SELECT * FROM [Sample File Parameter1]", 2
    Workbooks("Book1").Connections.Add2 _
        "Query - Transform Sample File from Pipeline Working1", _
        "Connection to the 'Transform Sample File from Pipeline Working' query in the workbook." _
        , Array( _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform Sample File from Pipeline Working"";Extended Propert" _
        , "ies="""""), "SELECT * FROM [Transform Sample File from Pipeline Working]", 2
    Workbooks("Book1").Connections.Add2 "Query - Sample File1", _
        "Connection to the 'Sample File' query in the workbook.", _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File"";Extended Properties=""""" _
        , "SELECT * FROM [Sample File]", 2
    Workbooks("Book1").Connections.Add2 _
        "Query - Transform File from Pipeline Working1", _
        "Connection to the 'Transform File from Pipeline Working' query in the workbook." _
        , _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform File from Pipeline Working"";Extended Properties=""""" _
        , "SELECT * FROM [Transform File from Pipeline Working]", 2
    Windows("Book3").Activate
    Range("A1").Select
End Sub

1 个答案:

答案 0 :(得分:0)

这将打开文件夹中的所有工作簿并将称为打印区域的范围复制到当前电子表格中-假定此代码在空白工作簿中

Sub ImportData()
 Const FolderToImport = "C:\Pipeline Working\" 'adjust as suits - keep last \ at end
 Dim s As String
 s = Dir(FolderToImport & "*.xl??")
 Do While s <> ""
       Dim wb As Workbook
       Set wb = Workbooks.Open(FolderToImport & s, False, True)
       wb.Sheets(1).Range("print area").Copy Destination:= _
         ThisWorkbook.Worksheets(1).Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
       wb.Close False
       s = Dir()
  Loop
End Sub