从文本,动态数据,查询表导入

时间:2015-04-20 18:37:04

标签: excel excel-vba vba

我有一个工作表,我从.diff文件导入txt,这里的数据是动态的(行是静态的,列是动态的)。

这是第1页,我选择全部并调用名称管理器。

在工作表2上,选择静态数据,调用名称管理器。

我选择了我的第3张,这是我的查询表的输出结果来自我的2张,使用MS查询(来自其他来源),从每张表中选择我需要的行,使用我的SQL语句并返回到MS Excel中。

我录制了这个宏。一个问题是动态数据,另一个是与前一个Excel工作表的连接,或者我认为,我必须在名称管理器中删除并重置连接。

从我发现的情况来看,编写代码似乎是要走的路,而且我不知道如何。

这个工作一次,但我需要每周都能这样做,我的第一张表中的数据是动态的,行保持静态。

'(Start with, I import my .Diff file to the first sheet)**
'(Here I choose my first sheet, select all, bring up Name Manager, (and here is what changes, the data fluctuates, can be 5 or up to 250 col))**  
ActiveWorkbook.Names.Add Name:="vcac", RefersToR1C1:="=vcac!R1C1:R50C31"
ActiveWorkbook.Names("vcac").Comment = ""
Sheets("ip").Select

'(here I choose my second sheet, this data never changes)**
ActiveWorkbook.Names.Add Name:="IP", RefersToR1C1:="=ip!R1C1:R11C3"
ActiveWorkbook.Names("IP").Comment = ""

'(Here I select my 3rd sheet, where the output of the QueryTable.  Everytime I import a new .Diff file, the QueryTable just doesn't work, headers, but no data.)**      
Sheets("InterestedParties").Select
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DSN=Excel Files;DBQ=C:\EXCEL VBA CODE\A\MACRO TEST2.xlsm;DefaultDir=C:\EXCEL VBA CODE\A;DriverId=1046;MaxBufferSize=2048;PageTi" _
        ), Array("meout=5;")), Destination:=Range("$A$1")).QueryTable
        .CommandText = Array( _
        "SELECT vcac.CI_CINUM, ip.`Group`, ip.Type" & Chr(13) & "" & Chr(10) & "FROM ip ip, vcac vcac" & Chr(13) & "" & Chr(10) & "WHERE (ip.OS='Windows') AND (vcac.CCIPERSONGROUP='EDC00002')" & Chr(13) & "" & Chr(10) & "UNION ALL" & Chr(13) & "" & Chr(10) & "SELECT vcac.CI_CINUM, ip.`Group`, ip.Type" & Chr(13) & "" & Chr(10) & "FROM ip ip, vcac v" _
        , "cac" & Chr(13) & "" & Chr(10) & "WHERE (ip.OS='Linux') AND (vcac.CCIPERSONGROUP='EDC00003')")
        .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_Query_from_Excel_Files"`enter code here`
        .Refresh BackgroundQuery:=False
    End With

End Sub

0 个答案:

没有答案