【问题标题】:VBA: Paste visible cells (filtered) into same rows but another columnVBA:将可见单元格(过滤)粘贴到同一行但另一列中
【发布时间】:2019-04-04 13:04:42
【问题描述】:

我想知道如何将过滤后的数据粘贴到相同的相应行但另一列中。

我在 Excel 中有一个过滤表,如下所示:

#Row    |      A      |     B
 1      |      foo    |  
 5      |      bar    | 
 8      |      fish   | 

现在我想将 A 列中的值粘贴到 B 列并保持行位置。

#Row    |     A       |     B
 1      |     foo     |     foo
 2      |             |
 3      |             | 
 4      |             |
 5      |     bar     |     bar 
 ...    |             |
 ...    |             |
 8      |    fish     |     fish

普通粘贴方法将值粘贴为连续块。

last = db_ws.Rows(Rows.Count).End(xlUp).Row
db_ws.Range("A11:BC" & last).SpecialCells(xlCellTypeVisible).Copy
tgt_ws.Range("A11").PasteSpecial

任何帮助表示赞赏。

【问题讨论】:

  • 您想在可见单元格上使用循环并将行存储为变量:stackoverflow.com/questions/10849177/…
  • 您是否按照db_wstgt_ws 这两个工作表变量的建议粘贴到不同的工作表上?
  • 感谢您的回复艾米丽。我刚刚意识到循环可能是这里唯一的解决方案。
  • @BigBen 嗨,抱歉这里的代码不清楚。实际上,在复制工作表tgt_ws 之前,我想对A 列中的过滤值进行存档/历史记录,并将它们粘贴到同一张工作表db_ws BC 列中
  • 知道了,额外的细节很有帮助。

标签: excel vba filter copy paste


【解决方案1】:

代码sn-p:

Dim i as Long
last = db_ws.Rows(Rows.Count).End(xlUp).Row

For i = 11 to last
If db_ws.Range("A"& i).EntireRowHidden=False Then
    tgt_ws.Range("A"& i).Value = db_ws.Range("A" & i).Value
    tgt_ws.Range("B" & i).Value = db_ws.Range("A" & i).Value
End If
Next i

【讨论】:

  • 感谢您的建议!同时,我将您的链接与阵列解决方案一起使用!两者都完美!
【解决方案2】:

为了完成这里的主题,您可以使用数组解决方案。

count = -1 ' array has to start with 0

On Error GoTo errHndlr:
For Each cl In rng.SpecialCells(xlCellTypeVisible)
    count = count + 1
    ReDim Preserve arr(count)
    arr(count) = cl.Row
    Debug.Print cl.Row
Next cl
errHndlr:

For Each Item In arr
    db_ws.Cells(Int(Item), 55) = db_ws.Cells(Int(Item), 1).Value
Next Item

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2017-01-23
    • 1970-01-01
    相关资源
    最近更新 更多