简体   繁体   English

在工作表之间选择和复制的VB Excel 2010代码

[英]VB Excel 2010 code to select and copy between sheets

I'm still learning VB. 我还在学习VB。 I tried recording a VB script in excel 2010 that selects a name on the main sheet, then goes to another sheet and finds all the rows with that name, copies all the rows and returns to the main sheet and insert the copied cells below the selected name. 我尝试在excel 2010中记录VB脚本,该脚本在主工作表上选择一个名称,然后转到另一工作表并找到具有该名称的所有行,复制所有行并返回到主工作表,然后将复制的单元格插入到所选的下面名称。 The cells are pushed down. 细胞被推下。 The code should repeat for the next name below where the copied cells were paste. 代码应重复以下粘贴粘贴复制的单元格的名字。

My recording failed to do all of the above. 我的录音未能完成上述所有操作。 Do you have a suggestion? 你有什么建议吗?

    Sub Macro5()
'
' Macro5 Macro
'
' Keyboard Shortcut: Ctrl+l
'
    Selection.Copy
    Sheets("Sheet1").Select
    ActiveCell.Offset(2, 3).Range("A1").Select
    Cells.Find(What:="Leeanne Hickmott", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Rows("1:3").EntireRow.Select
    ActiveCell.Offset(0, -7).Range("A1").Activate
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet4").Select
    ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
    Selection.Insert Shift:=xlDown
    ActiveCell.Offset(3, 6).Range("A1").Select
End Sub

Excel's record macro process can't record the conditionals or loops you need to make that kind of macro work. Excel的记录宏过程无法记录使这种宏起作用所需的条件或循环。 It also can't record that you pasted a value into the find dialog, it just records that you want to search for "Leeanne Hickmott" 它也无法记录您将值粘贴到“查找”对话框中,而只是记录您要搜索“ Leeanne Hickmott”

My first suggestion would be to use range variables to point to the important cells on each worksheet. 我的第一个建议是使用范围变量指向每个工作表上的重要单元格。 For example... 例如...

Dim rngPasteHere as Range, rngCopyFrom as Range
set rngPasteHere = Selection
set rngCopyFrom = Sheets("Sheet1").Range("D1")
' find first row
set rngCopyFrom = rngCopyFrom.Cells.Find(What:=rngPasteHere.Value _
                             , After:=rngCopyFrom, LookIn:=xlFormulas _
                             , LookAt:=xlPart, SearchOrder:=xlByRows _
                             , SearchDirection:=xlNext, MatchCase:=False _
                             , SearchFormat:=False)

You will also need two loops, an outer loop that repeats the whole process for the next name and an inner loop that finds all the rows with the current name. 您还将需要两个循环,一个外部循环重复整个过程以使用下一个名称,一个内部循环查找使用当前名称的所有行。 I can't really get more specific than that without knowing what your data looks like. 在不知道您的数据是什么样的情况下,我无法提供比这更具体的信息。

  1. Are the names sorted alphabetically? 名称是否按字母顺序排序?
  2. Are all rows with the same name right next to each other or are they mixed with other names? 所有具有相同名称的行是否都紧挨着,还是与其他名称混合在一起?
  3. Are there just a few rows of each name or thousands? 每个名称只有几行还是几千行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM