VBA - 使用查询更新现有的Excel文件

时间:2016-10-17 12:49:29

标签: excel vba excel-vba

我有一个excel文件并使用此代码导入数据。

https://www.youtube.com/watch?v=sQIImQbEO_Q

与此示例不同,我的Datafeeds工作表已经具有以特定方式格式化的数据。如果我重新运行宏,现有数据将转移到L1(即第二个csv结束的位置),并从A1写入新数据。 1)我想修改代码来替换现有的代码覆盖它。 2)现有的格式也以特定方式格式化。我希望保留格式 我怎样才能最好地修改我的代码来做到这一点?

这是代码:

Sub AutomateImport()
For rep = 4 To 16

Dim file_name As String
Dim row_number As String
Dim output_sheet As String

file_name = Sheets("Admin").Range("B" & rep).Value
output_sheet = Sheets("Admin").Range("C" & rep).Value
row_number = Sheets("Admin").Range("D" & rep).Value
Sheets(output_sheet).UsedRange.ClearContents

    With Sheets(output_sheet).QueryTables.Add(Connection:="TEXT;" + file_name, Destination:=Sheets(output_sheet).Range("$A$" + row_number))
        .FieldNames = True
        .RowNumbers = True
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

Next rep

MsgBox "Done"
End Sub

0 个答案:

没有答案