为什么第二个表范围没有转移到目标工作表?

时间:2018-08-29 13:10:12

标签: excel vba excel-vba

我有一个脚本,将两个工作表组合成一个全面的工作表:legal_ws + cib_ws ---> comp_ws

legal_ws可以很好地移动:

        lrow = legal_ws.Range("A" & Rows.Count).End(xlUp).Row
        lcol = legal_ws.Cells(1, legal_ws.Columns.Count).End(xlToLeft).Column

Set legal_rng = legal_ws.Range(legal_ws.Cells(2, 1), legal_ws.Cells(lrow, lcol))

还有这个

legal_rng.Copy Destination:=comp_ws.Range("A2")

,但是以相同方式传输cib_ws的方法无效。 cib_ws传输需要自动过滤器,并且仅复制可见的单元格减去标题,但不会复制这些单元格。

        lrow2 = cib_ws.Range("A" & Rows.Count).End(xlUp).Row
        lcol2 = cib_ws.Cells(1, cib_ws.Columns.Count).End(xlToLeft).Column

Set cib_rng = cib_ws.Range(cib_ws.Cells(2, 1), cib_ws.Cells(lrow, lcol)).SpecialCells(xlCellTypeVisible)

        With cib_ws.Range("A1" & lcol2)
                    .AutoFilter
                    .AutoFilter Field:=61, Criteria1:="Regional Presidents"
        End With

            cib_rng.Copy Destination:=comp_ws.Range("A" & lrow3 + 1)

为什么cib_ws传输不起作用,而legal_ws却有效?

这是整个脚本:

    Sub TCR_Update()



    'File Paths
    Dim Legal As String, CIB As String, Comp_TCR As String

    Legal = "M:\Legal-TCR-Template.xlsx"
    CIB = "M:\CIB-TCR-Template.xlsx"
    Comp_TCR = "M:\Total_TCR.xlsx"

    Dim legal_wb As Workbook, cib_wb As Workbook, comp_wb As Workbook
    Set legal_wb = Workbooks.Open(Filename:=Legal)
    Set cib_wb = Workbooks.Open(Filename:=CIB)
    Set comp_wb = Workbooks.Open(Filename:=Comp_TCR)

    Dim legal_ws As Worksheet, cib_ws As Worksheet, comp_ws As Worksheet
    Set legal_ws = legal_wb.Sheets("ps")
    Set cib_ws = cib_wb.Sheets("ps")
    Set comp_ws = comp_wb.Sheets("Sheet1")

    Dim lrow As Long, lcol As Long, lrow2 As Long, lcol2 As Long, lrow3 As Long

    Dim legal_rng As Range



            lrow = legal_ws.Range("A" & Rows.Count).End(xlUp).Row
            lcol = legal_ws.Cells(1, legal_ws.Columns.Count).End(xlToLeft).Column
            lrow2 = cib_ws.Range("A" & Rows.Count).End(xlUp).Row
            lcol2 = cib_ws.Cells(1, cib_ws.Columns.Count).End(xlToLeft).Column
           lrow3 = comp_ws.Range("A" & Rows.Count).End(xlUp).Row

                Set legal_rng = legal_ws.Range(legal_ws.Cells(2, 1), legal_ws.Cells(lrow, lcol))
                Set cib_rng = cib_ws.Range(cib_ws.Cells(2, 1), cib_ws.Cells(lrow, lcol)).SpecialCells(xlCellTypeVisible)


        legal_rng.Copy Destination:=comp_ws.Range("A2")

            With cib_ws.Range("A1" & lcol2)
                        .AutoFilter
                        .AutoFilter Field:=61, Criteria1:="Regional Presidents"
            End With

                cib_rng.Copy Destination:=comp_ws.Range("A" & lrow + 1)

              legal_wb.Close SaveChanges:=False
              cib_wb.Close SaveChanges:=False

            Cells.Select
            Cells.Copy
            Cells.PasteSpecial Paste:=xlPasteValues


              With comp_ws
              .Cells.WrapText = False
              .Rows.AutoFit
              .Columns.AutoFit
              End With

2 个答案:

答案 0 :(得分:0)

主要问题:

  1. 使用lrow2lcol2代替lrowlcol(必须只是一个简单的疏忽)。
  2. 正如@Scott Craner在评论中指出的,With cib_ws.Range("A1" & lcol2)指的是一个单元格,而不是要自动过滤的整个范围。可能应该更改为With cib_ws.Range("A1", cib_ws.Cells(Lrow2,lcol2))
  3. 切换顺序:首先自动过滤,然后然后 Set cib_rng等于生成的可见单元格。请注意,在尝试复制之前,您需要测试是否有可见的单元格,否则cib_rng将是Nothing。因此,将您的cib_rng.Copy语句包装在If Not cib_rng Is Nothing Then...End If中。

答案 1 :(得分:0)

尝试一下:

Sub TCR_Update()
    lrow2 = cib_ws.Range("A" & Rows.Count).End(xlUp).Row
    lcol2 = cib_ws.Cells(1, cib_ws.Columns.Count).End(xlToLeft).Column

    Set cib_rng = cib_ws.Range(cib_ws.Cells(2, 1), cib_ws.Cells(lrow, lcol)).SpecialCells(xlCellTypeVisible)

    cib_ws.AutoFilterMode = False

    With cib_ws.Range("A1" & lcol2)
            .AutoFilter
            .AutoFilter Field:=61, Criteria1:="Regional Presidents"
            .SpecialCells(xlCellTypeVisible).Copy Destination:=comp_ws.Range("A" & lrow3 + 1)
    End With

    'cib_rng.Copy Destination:=comp_ws.Range("A" & lrow + 1)
End Sub