1

我找到了这段代码,并尝试对其进行更改,以便将导入的数据存储在另一张表中。

第一个代码是Destination:=ActiveCell,我尝试更改它,以便将数据存储在另一张表中。我也试过这个:Destination:=Workbook.Sheets(CSV).Cells(1, 1))但它也不起作用。

我也在寻找代码来自动选择名为 export-price 的最新 csv 文件,但我还没有找到解决方案。

Sub LoadProducts()
    Dim fileName As String, folder As String

    folder = "C:\Users\CP\Downloads\"
    fileName = ActiveCell.Value

    ActiveCell.Offset(1, 0).Range("A1").Select


    With ActiveSheet.QueryTables _
        .Add(Connection:="TEXT;" & folder & fileName, Destination:=Workbook.Sheets(CSV).Cells(1, 1))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
4

1 回答 1

2

您需要正确声明您的对象,然后使用它们。这是一个例子。根据需要进行更改。

Dim wb As Workbook, thiswb As Workbook
Dim ws As Worksheet, thisws As Worksheet

'~~> Change as applicable
Set thiswb = ThisWorkbook
Set thisws = thiswb.Sheets("Sheet1")

CSV = "Sheet1"
Set wb = Workbooks.Open("C:\Blah Blah.xlsx")
Set ws = wb.Sheets(CSV)

With thisws.QueryTables _
    .Add(Connection:="TEXT;" & folder & Filename, Destination:=ws.Cells(1, 1))
于 2013-10-16T13:01:11.233 回答