【问题标题】:Copying cell values from one sheet to another, and paste it near a cell with specific value将单元格值从一张表复制到另一张表,并将其粘贴到具有特定值的单元格附近
【发布时间】:2021-05-27 21:09:41
【问题描述】:

我有一个固定的工作任务,我需要将一个数字列表复制到另一张纸上。在该表中,我需要将这些数字一一粘贴到具有特定值的单元格右侧的单元格中(在列中重复)。 (请注意,目标表按该值排序 -"מודל תגובה" 并且存在隐藏行。

这很难解释,所以我希望图片可以。

我尝试编写合适的代码,但我不断收到不同的错误。 将单元格值复制到目标单元格时似乎出现问题。

Dim i As Integer
i = 4

Do While IsEmpty(Cells(i, 1).Value) = False
    Worksheets(1).Select
    Cells(i, 1).Copy
    Worksheets(2).Select
    Cells.Find(What:="מודל תגובה", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset(0, -1).Activate

    If IsEmpty(ActiveCell.Value) = False Then
         Selection.FindNext(After:=ActiveCell).Activate
         ActiveCell.Offset(0, -1).Paste
    Else
         ActiveCell.Offset(0, -1).Select
         ActiveCell.Paste  
    End If

    i = i + 1
Loop

抱歉代码很糟糕(字面意思是我的第一个宏)。

【问题讨论】:

  • “我不断收到不同的错误” 您在哪一行代码中遇到了哪个错误?不知道哪里出了问题就很难提供帮助 • 阅读How to avoid using Select in Excel VBA 可能会让您受益。

标签: excel vba


【解决方案1】:

解决方案是仅循环通过过滤范围的可见单元格。

在运行此代码之前,请确保已针对 "מודל תגובה" 过滤目标。在运行此代码之前,它需要看起来像您的第二张图片。

Dim SourceSheet As Worksheet
Set SourceSheet = Worksheets(1)

Dim DestinationSheet As Worksheet
Set DestinationSheet = Worksheets(2)

Dim LastRow As Long
LastRow = DestinationSheet.Cells(DestinationSheet.Rows.Count, "B").End(xlUp).Row

Dim VisibleCells As Range
On Error Resume Next 'next line errors if no visible cells so we turn error reporting off
Set VisibleCells = DestinationSheet.Range("A2", "A" & LastRow).SpecialCells(xlCellTypeVisible)
On Error Goto 0 'turn error reporting on or you won't see if other errors occur

If VisibleCells Is Nothing Then  'abort if no cells are visible in the filter
    MsgBox "No cells to paste at"
    Exit Sub
End If

Dim SourceRow As Long
SourceRow = 4   'start row in your source sheet

Dim Cell As Range
For Each Cell In VisibleCells.Cells    'loop through visible cells
    Cell.Value = SourceSheet.Cells(SourceRow, "A").Value 'copy value
    SourceRow = SourceRow + 1  'incerease source row
Next Cell

确保使用工作表名称定义 DestinationSheetSourceSheet

【讨论】:

  • 我不能使用 Worksheet(1)(2) 吗?
  • @yachninho 是为了什么?在哪里?比半句话多一点的信息可能会有所帮助。 • 如果您的意思是源和目标,是的,您可以。但是,如果有人将您的工作表(1)移动到位置(2),您的代码就会失败。 Worksheet(1) 表示取行中第一个选项卡的任何工作表。任何人都可以轻松更改它,您的数据就会变得混乱。
  • 对不起。是的,我的意思不是来源和目的地。问题是工作表名称将随每个文件而变化。非常感谢您的帮助
  • @yachninho 那么这个职位当然是更好的选择。请参阅我的编辑。如果这回答了您的问题,请点赞/标记为已解决:Accepting Answers: How does it work?
【解决方案2】:

试试这个:

Dim i As Integer
Dim Last_Row as Long
Worksheets(1).Select
'The "1" Of the line below means that the variable gonna count the rows of the first column (A)
Last_Row = Application.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:A" & Last_Row).Copy
Worksheets(2).Select
Range("A1").Select
ActiveSheet.Paste

【讨论】:

  • 小心目的地显示过滤表(见蓝色数字!)有隐藏的行。如果您粘贴到 A1 中,它也会粘贴到隐藏行中,而不仅仅是可见行。这会弄乱数据。
  • 是的,隐藏行是宏的原因/
  • @David 试过了,它在 ActiveCell.Paste 上出现错误“对象不支持此属性或方法”
  • 谢谢@yachninho,我已经更正了
猜你喜欢
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2022-10-08
  • 2020-01-13
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
相关资源
最近更新 更多