excel - VBA Excel - 想要根据选定的单元格选择 1 行或多行并复制到另一张工作表

标签 excel vba

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .


1年前关闭。







Improve this question




原谅我的无知,这是我第一次真正潜入vba。
我有 3 张床单
Master 包含一个包含价格和属性的目录
plist 包含一个我使用教程创建的搜索按钮来过滤结果。我希望用户能够选择一个或多个显示的单元格,然后单击一个按钮将行复制到新工作表
新建是选定文件的最终目的地。行将按顺序复制,当它们被点击时,从上到下。
我已经搜索并尝试了几个想法,但我的无知确实阻碍了我前进。希望有一个搜索方向或您可以提供的任何帮助。
TLDR:需要代码将选定单元格中的值复制到单独的工作表中。

最佳答案

非相邻单元格的行

  • 将完整的代码复制到标准模块中,例如Module1 .
  • 只运行(或分配给一个按钮)第一个过程copyRowsOfSelectedCells .如果从 VBE 运行,请确保工作表 plist被选中(激活)。 'plist' 也是包含按钮的工作表。
  • 必要时由第一个程序调用随附的程序。
  • 在每个随附过程之前,都有一个方法、属性或函数名称,它们与以下过程最相关,以完成手头的任务。研究(“谷歌”)那些以更好地理解每个程序。

  • 代码
    Option Explicit
    
    Sub copyRowsOfSelectedCells()
        
        ' Define Destination Worksheet Name.
        Const dstName As String = "New"
        
        ' Test if Selection is a range (object).
        If Not isRange(Selection) Then
            GoTo ProcExit
        End If
        
        ' Define Source Row Ranges.
        Dim rng As Range
        Set rng = CollectedRowRanges(Selection)
            
        ' Define Destination First Cell Range.
        Dim cel As Range
        Set cel = FirstCell(rng.Worksheet.Parent.Worksheets(dstName))
        
        ' Copy from Source Worksheet to Destination Worksheet.
        copyRowsToAnotherWorksheet rng, cel
        
    ProcExit:
    
    End Sub
    
    ' TypeName Function
    Private Function isRange(PossibleRange As Variant) _
      As Boolean
        If TypeName(PossibleRange) = "Range" Then
            isRange = True
        Else
            Debug.Print "Not a range."
        End If
    End Function
    
    ' Range.Areas Property
    Private Function CollectedRowRanges(SourceRange As Range) _
      As Range
        Dim bRng As Range
        Dim rng As Range
        Dim cel As Range
        For Each rng In SourceRange.Areas
            For Each cel In rng.Cells
                buildRange bRng, cel.EntireRow
            Next cel
        Next rng
        Set CollectedRowRanges = bRng
    End Function
    
    ' Application.Union Method
    Private Sub buildRange(ByRef BuiltRange As Range, _
                           AddRange As Range)
        If Not BuiltRange Is Nothing Then
            Set BuiltRange = Union(BuiltRange, AddRange)
        Else
            Set BuiltRange = AddRange
        End If
    End Sub
    
    ' Range.Find Method
    Private Function FirstCell(Sheet As Worksheet, _
                               Optional ByVal ColumnIndex As Variant = "A") _
      As Range
        Dim cel As Range
        Set cel = Sheet.Cells.Find(What:="*", _
                                   LookIn:=xlFormulas, _
                                   SearchOrder:=xlByRows, _
                                   SearchDirection:=xlPrevious)
        If Not cel Is Nothing Then
            Set FirstCell = Sheet.Cells(cel.Row, ColumnIndex).Offset(1)
        Else
            Set FirstCell = Sheet.Cells(1, ColumnIndex)
        End If
    End Function
    
    ' Range.PasteSpecial Method
    Private Sub copyRowsToAnotherWorksheet(CopyRows As Range, _
                                           PasteCell As Range)
        If Not CopyRows Is Nothing And Not PasteCell Is Nothing Then
            Dim PasteRowCell As Range
            Set PasteRowCell = PasteCell.Cells(1).Offset(, 1 - PasteCell.Column)
            CopyRows.Copy
            PasteCell.Worksheet.Activate
            PasteRowCell.PasteSpecial xlPasteValues
            Application.CutCopyMode = False
        End If
    End Sub
    

    关于excel - VBA Excel - 想要根据选定的单元格选择 1 行或多行并复制到另一张工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64839579/

    相关文章:

    excel - 在 Excel 工作表名称中使用井号 (#)

    带有 VBA 的 Excel MsgBox 用于多个链接范围

    excel - VBA - 避免循环

    excel - ISNA + Vlookup 功能不起作用

    excel - 向自动 Outlook 邮件添加签名

    c# - 自定义 Excel 右键单击​​上下文菜单被快速分析默认值覆盖

    vba - Word VBA 检索 IP 地址 "silently"

    excel - 为细胞着色的另一种更快的方法

    VBA Excel Outlook 电子邮件正文格式

    vba - 创建新的命名范围时触发宏?