简体   繁体   English

根据单元格值将数据从一张纸移动到另一张纸

[英]Moving Data from One Sheet to Another Based on Cell Value

I have a Spreadsheet "upload" I run a macro to compile the data on the sheet. 我有一个电子表格“上载”,我运行一个宏来编译工作表上的数据。 I have a column "D" which attributes the data to client. 我有一列“ D”,该列将数据归因于客户端。 I want to look for a specific client and automatically move those rows to another worksheet. 我想寻找一个特定的客户端,并自动将这些行移动到另一个工作表。 I have tried this code, but I am making an error "Upload.Range("D1", Upload.Range("D" & Upload.Rows.Count)" 我已经尝试过此代码,但出现错误“ Upload.Range(“ D1”,Upload.Range(“ D”&Upload.Rows.Count)“

I anticipate future clients information to need be separated from the initial spreadsheet as well. 我预计将来的客户信息也需要与初始电子表格分开。

Any Help would be much appreciated 任何帮助将非常感激

Sub TransferData()

        Dim ar As Variant
        Dim i As Integer
        Dim lr As Long

ar = Array("3032")

Application.ScreenUpdating = False
Application.DisplayAlerts = False

  For i = 0 To UBound(ar)
         Upload.Range("D1", Upload.Range("D" & Upload.Rows.Count).End(xlUp)).AutoFilter 1, ar(i), 4, , 0
         lr = Upload.Range("D" & Rows.Count).End(xlUp).Row
         If lr > 1 Then
         Upload.Range("A2", Upload.Range("G" & Upload.Rows.Count).End(xlUp)).Copy Sheets(ar(i)).Range("A" & Rows.Count).End(3)(2)
         Upload.Range("A2", Upload.Range("G" & Upload.Rows.Count).End(xlUp)).Delete
         Sheets(ar(i)).Columns.AutoFit
         End If
    Next i
[G1].AutoFilter

Application.DisplayAlerts = True
Application.CutCopyMode = False
Application.ScreenUpdating = True
MsgBox "Data transfer completed!", vbExclamation, "Status"

End Sub

There is a substantial difference between the worksheet Name property and the worksheet Codename property. 工作表的“名称”属性和工作 表的“代码名”属性之间存在实质性差异。

While it is possible to change the worksheet's Codename, it isn't a common practice and if you are unsure then it is most likely that you are referring to the worksheet Name property. 虽然可以更改工作表的代号,但这不是常见的做法,如果不确定,则很可能是在引用工作表的Name属性。

Your narrative says nothing about wanting the 'bottom 10 results' but your code uses 4 for the xlBottom10Items operator (see xlAutoFilterOperator enumeration ). 您的叙述并没有说要获得“最低10个结果”,但是您的代码对xlBottom10Items运算符使用4 (请参见xlAutoFilterOperator枚举 )。

I have no idea what the 3 in Sheets(ar(i)).Range("A" & Rows.Count).End(3)(2) is intended to represent. 我不知道在3 Sheets(ar(i)).Range("A" & Rows.Count).End(3)(2)旨在表示。 I would suppose that you meant xlUp which has a numerical value of -4162 . 我想您的意思是xlUp ,其数值为-4162 (see xlDirection enumeration ). (请参阅xlDirection枚举 )。

Sub TransferData()

    Dim ar As Variant
    Dim i As Long, lr As Long

    ar = Array("3032")

    ' ... app environment settings removed for brevity

    'reference the filter worksheet properly
    With Worksheets("Upload")
        lr = .Range("D" & Rows.Count).End(xlUp).Row
        If .AutoFilterMode Then .AutoFilterMode = False
        For i = LBound(ar) To UBound(ar)
            'there was no mention of 'bottom 10 items in your narrative but your code shows that option
            With .Range("D1:D" & lr)
                '.AutoFilter field:=1, Criteria1:=ar(i), _
                            Operator:=xlBottom10Items, VisibleDropDown:=False
                .AutoFilter field:=1, Criteria1:=(ar(i)), VisibleDropDown:=False

                With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                    If CBool(Application.Subtotal(103, .Cells)) Then
                        .Offset(0, -3).Resize(, 7).Copy _
                          Destination:=Worksheets(ar(i)).Range("A" & Rows.Count).End(xlUp)(2)
                        Worksheets(ar(i)).Columns.AutoFit
                        .Delete shift:=xlUp
                    End If
                End With
            End With
        Next i
        If .AutoFilterMode Then .AutoFilterMode = False
    End With

    ' ... app environment settings removed for brevity

    MsgBox "Data transfer completed!", vbExclamation, "Status"

End Sub

That should get you started. 那应该让您开始。 It seems you still have a few decisions o make based on my notes. 根据我的笔记,您似乎仍有一些决定要做出。

Application.CutCopyMode = False Application.CutCopyMode =假

See Should I turn .CutCopyMode back on before exiting my sub procedure? 请参阅退出子过程之前是否应该重新打开.CutCopyMode? .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据第一张工作表中第二个单元格的值,将数据从一个单元格复制到另一张工作表中的单元格? - Copy data from one cell to a cell in another sheet based on the value of a second cell in the first sheet? 根据单元格值将数据从一张表复制并粘贴到另一张表 - Copy and paste data from one sheet to another sheet(s) based on cell value 根据单元格值从另一张表中提取数据 - Pull data based from another sheet based on a cell value excel根据另一个单元格值从另一个工作表中提取数据 - excel pulling data from another sheet based on another cell value 根据单元格值将行从一个 Excel 工作表复制到另一个 - Copying rows from one Excel sheet to another based on cell value 根据条件将单元格数据从一张纸复制到另一张纸 - copy cell data from one sheet to another based on condtion 根据单元格值将数据从特定列复制到另一个工作表 - Copy data from specific columns to another sheet based on a cell value 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 根据另一个单元格的值将数据从一个单元格复制到另一个单元格 - Copying data from one cell to another based on the value of another cell 使用单元格值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another with the cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM