【问题标题】:Excel VBA copy filtered CurrentRegion visible cells but exclude last 3 columnsExcel VBA 复制过滤的 CurrentRegion 可见单元格,但排除最后 3 列
【发布时间】:2020-03-04 13:21:06
【问题描述】:

有一个小问题。我有一大段代码,它采用用户选择的参考编号,并通过过滤然后复制数据来定位多个其他工作表上的相应行(可以有多行或没有定位)。

这很好用,除了当我真的希望它复制列 A 到 K 时它复制所有可见数据(列 AN)(因为粘贴表上的 L 到 N 是我有设置参考号的公式的地方,因此不能被粘贴)。

我已经尝试对以下代码部分进行一些更改,包括偏移量,但是它要么忽略偏移量(可能是因为我使用的是 xlCellTypeVisible,我必须这样做,因为需要复制的数据可以跨越多个非顺序行),否则我收到有关不支持选择方法的错误。

有什么想法吗?

正在复制的工作表 - DbExtract 数据粘贴到的工作表 - DuplicateRecords 谢谢。

Sub UpdateInputWithExisting()

' Other code that works using set with values and active cell offset with values for other sheets

Sheets("TK_Register").Range("A1").CurrentRegion.AutoFilter field:=12, Criteria1:=RefID

Dim DbExtract, DuplicateRecords As Worksheet
Set DbExtract = ThisWorkbook.Sheets("TK_Register")
Set DuplicateRecords = ThisWorkbook.Sheets("EditEx")

DbExtract.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).copy
DuplicateRecords.Cells(31, 3).PasteSpecial xlPasteValues
On Error Resume Next
Sheets("TK_Register").ShowAllData
On Error GoTo 0

ActiveWorkbook.RefreshAll
Sheets("EditEx").Select
ActiveWindow.SmallScroll Down:=-120
Range("B14:M14").Select

MsgBox ("Record Retrieved. Make your changes and ensure you click 'Save Changes' to update the Master Registers")

End Sub

【问题讨论】:

    标签: excel vba filter copy


    【解决方案1】:

    需要使用.Resize方法。替换这一行:

    DbExtract.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).copy
    

    有了这个:

    With DbExtract.Range("A1").CurrentRegion
        .Resize(, .Columns.Count - 3).SpecialCells(xlCellTypeVisible).Copy
    End With
    

    【讨论】:

      猜你喜欢
      • 2012-12-19
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多