将过滤的数据复制到另一个工作簿

时间:2020-02-28 19:01:48

标签: excel vba

我正在尝试将过滤后的数据复制到另一个工作簿中,这给了我运行时错误'1004'。

import json


@client.event
async def on_message(message):
    global data
    guild=client.get_guild(646323823502950446)
    data=len(guild.members)
    channel=client.get_channel(646323823507144710)
    if message.author.bot==False:
        with open('stats1.json', 'w') as file:
            json.dump(data, file)


@client.command()
async def memberstats(ctx):
    with open('stats1.json', 'r') as read_file:
        data1=json.load(read_file)
    await ctx.send(data1)

1 个答案:

答案 0 :(得分:0)

请注意,您的工作簿路径指向一个文件夹,您需要将其指向一个文件。

编辑:如您的评论所述

targetWorkbookPath = "H:\L\Roy\H AND E\2020\SAP - ZPSD02_template2.xlsx"(或xlsm)

检查代码的注释并进行调整以满足您的需求

编辑2:使用了在“编辑”中添加的路径,并将其合并到代码中。

代码:

Sub DS()

    Dim sourceWorkook As Workbook
    Dim targetWorkbook As Workbook
    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet

    Dim sourceWorkbookPath As String
    Dim targetWorkbookPath As String
    Dim lastRow As Long


    ' Define workbooks paths
    sourceWorkbookPath = "H:\L\Roy\RT\Transfers\Transfers 2020 - Roy.xlsm"
    targetWorkbookPath = "H:\L\Roy\H and E\2020\SAP - ZPSD02_template2.xlsx"

    ' Set a reference to the target Workbook and sheets
    Set sourceWorkbook = Workbooks.Open(sourceWorkbookPath)
    Set targetWorkbook = Workbooks.Open(targetWorkbookPath)

    ' definr worksheet's names for each workbook
    Set sourceSheet = sourceWorkbook.Worksheets("ST TO ST")
    Set targetSheet = targetWorkbook.Worksheets("Sheet1")

    With sourceSheet

        ' Get last row
        lastRow = .Range("J" & .Rows.Count).End(xlUp).Row

        .Range("A1:O1").AutoFilter Field:=12, Criteria1:="PENDING"
        .Range("A1:O1").AutoFilter Field:=10, Criteria1:="U3R", Operator:=xlOr, Criteria2:="U2R"

        .Range("J2:J" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
                                     Destination:=targetSheet.Range("A1")
        .Range("C2:C" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
                                     Destination:=targetSheet.Range("B1")
        .Range("D2:D" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
                                     Destination:=targetSheet.Range("E1")
        .Range("H2:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
                                     Destination:=targetSheet.Range("F1")
    End With
End Sub

让我知道它是否有效