【问题标题】:How to copy specific cells from each row in another sheet if certain condition is met?如果满足特定条件,如何从另一张表中的每一行复制特定单元格?
【发布时间】:2014-02-18 13:52:35
【问题描述】:

所以我的问题是这样的。我有一个工作簿,可以说 2 张。我已经从另一个程序和 sheet1 自动创建了 sheet2,我只想要 sheet2 中的一些信息。

我现在正在尝试创建一个宏,它将检查从 14 开始的每一行,并且 E% 中的值大于 15。如果满足条件,我希望宏将单元格值从 C% 和 E% 复制到sheet1 让我们在 A5 和 B5 中说,然后继续到 sheet2 中的下一行,将值粘贴到 A6 B6 等等。

Sub Test()
    Dim rng As Range
    Dim lastRow As Long
    Dim cell As Variant


    With Sheets("Sheet2")


        lastRow = .Range("E" & .Rows.Count).End(xlUp).Row
        Set rng = .Range("E14:E" & lastRow)


        For Each cell In rng
            If cell.Value > 15 Then
            'And here is where it gets bugged. I know theres something wrong with the .select but I couldnt think of any other way to
            'pick only just the 2 cells needed.
                Range(cell.Offset(0, -1), cell.Offset(0, 0)).Select
                Selection.Copy

                'In here there should also be some code to select where to place the copyed
                'data but since it already got bugged couldnt really find a solution for 
                'it..
                Sheets("Sheet1").Select
                ActiveSheet.Paste
                Sheets("Sheet2").Select

            End If
        Next
    End With


End Sub

【问题讨论】:

  • sooo 你在哪里?你试过什么,你遇到了什么错误?没有人会为您编写宏代码,但请发布您的代码并告诉我们您遇到了什么错误,我们将帮助您完成
  • 是的,对不起,我的错......在一秒钟内获得了原始代码
  • 嗯,那似乎发生了什么?当我运行代码时,它并没有真正失败。单元格为空白时会卡住吗?或者当它们是文本而不是数字时?我在第 14 行到第 28 行的 e 列中粘贴了各种数字,然后循环遍历所有数字。单元格设置正确,它选择了 e 列中的单元格和该列左侧的单元格(d 列,如果要选择右侧,请在 offset 中使用正数)它复制了选择,它激活了 sheet1(虽然我会使用 .activate 代替)并粘贴,然后回到 sheet2
  • 而不是选择/复制,您可以尝试Range(cell.Offset(0, -1), cell.Offset(0, 0)).copy 粘贴时,您希望实际选择粘贴位置,因此在激活目标工作表后,您会想要Range("a5", "b5").pasteSpecial。你会使用PasteSpecial 作为范围对象不支持粘贴方法,只有当你只是像 A1 或其他东西一样粘贴时,activesheet.paste 才真正起作用。

标签: excel vba


【解决方案1】:

所以我想我会把它放在一起:

Sub Test()
Dim rng As Range
Dim lastRow As Long
Dim cell As Variant
dim count as long
count = 0

With Sheets("Sheet2")


    lastRow = .Range("E" & .Rows.Count).End(xlUp).Row
    Set rng = .Range("E14:E" & lastRow)


    For Each cell In rng
        If cell.Value > 15 Then
        'And here is where it gets bugged. I know theres something wrong with the         .select but I couldnt think of any other way to
        'pick only just the 2 cells needed.
            Range(cell.Offset(0, -1), cell.Offset(0, 0)).Select
            Selection.Copy
        'maybe use: Range(cell.Offset(0, -1), cell.Offset(0, 0)).copy


            'In here there should also be some code to select where to place the copyed
            'data but since it already got bugged couldnt really find a solution for 
            'it..
            Sheets("Sheet1").Activate
            Range("A5", B5).offset(count, 0).PasteSpecial 'this will make it so that it starts in a5, and moves down a row each time
            count = count + 1            'dont forget to increment count

            Sheets("Sheet2").Activate

        End If
    Next
End With


End Sub

这是一件很艰难的事情..

您可能会包含一些错误处理,例如:if not cell.value = "" then 或 if not isNumeric(cell.value) then,它们一起将确保您只处理带有数字的非空白单元格。

【讨论】:

  • 谢谢先生。通过一点点调整,我得到了它完美的工作。有人帮助新鲜大脑是件好事。我的从今天开始煮得太熟了,今天想不出一个想法,最后期限在 3 小时内推...
  • 如果单元格是数字,则不能为空。它必须包含一个数字,以便 Excel 将其视为数字,因此您只需要一次检查 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多