简体   繁体   English

如何根据单元格选择复制行的固定部分,Excel vba?

[英]How to copy fixed part of rows based on cell selection , Excel vba?

I have table with many columns, I need if I select cells B3 or C3 (for example) to copy cells B3:G3 on the same row.我有很多列的表,如果我需要 select 单元格 B3 或 C3(例如)复制同一行上的单元格 B3:G3。

Selection can be any rows on the sheet.选择可以是工作表上的任何行。 Is it possible to be done with vba?可以用 vba 完成吗? sample sheet is attached on this link https://easyupload.io/8knzr9样品表附在此链接上https://easyupload.io/8knzr9

在此处输入图像描述

Select Row Range Select 行范围

  • When selecting a range that contains cells in columns B and/or C , it will select their rows, but in columns B:G . When selecting a range that contains cells in columns B and/or C , it will select their rows, but in columns B:G .
  • It will not include the first two rows which contain merged cells.它不包括包含合并单元格的前两行。
  • It will allow multiple non-contiguous selections.它将允许多个不连续的选择。

Sheet Module eg Sheet1工作表模块,例如Sheet1

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Const cFirstRow As String = "B3:C3"
    Const sCols As String = "B:G"
    
    Dim crg As Range
    With Range(cFirstRow)
        Set crg = .Resize(Rows.Count - .Row + 1)
    End With
    
    Dim irg As Range: Set irg = Intersect(crg, Target)
    
    If Not irg Is Nothing Then
    
        Dim srg As Range, arg As Range, rrg As Range
        For Each arg In irg.Areas
            For Each rrg In arg.Rows
                If srg Is Nothing Then
                    Set srg = Columns(sCols).Rows(rrg.Row)
                Else
                    Set srg = Union(srg, Columns(sCols).Rows(rrg.Row))
                End If
            Next rrg
        Next arg
        
        If Not srg Is Nothing Then
            srg.Select
        End If
    
    End If
        
End Sub

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

相关问题 如何根据单元格选择 select 固定部分行,Excel vba? - How to select fixed part of rows based on cells selection , Excel vba? Excel VBA基于包含文本部分的单元格值删除行 - Excel VBA Delete Rows Based on Cell Value containing Part of a Text Excel VBA-如何基于两个单元格值添加行数并从第三个单元格复制/保留数据? - Excel VBA - How to add number of rows based on two cell values and copy/keep data from the third? 如何使用 VBA 根据值和 combobox 选择填充 excel 中的行? - How to populate rows in excel based on value and combobox selection using VBA? 如何在Excel VBA中根据给定的日期复制单元格值 - How to copy cell value based on the given Date in Excel VBA Excel VBA基于单元格值插入行,并根据该数字向下复制列 - Excel VBA to insert rows based on cell value and copy columns down based on that number 如何创建函数以根据VBA EXCEL中的单元格值隐藏行 - How to create a function to hide rows based on a cell value in VBA EXCEL 如何根据条件使用Excel VBA复制行 - How to copy rows using Excel VBA based upon a condition Excel VBA如何根据不同工作表中单元格的值删除行 - Excel VBA How to delete rows based on the value of a cell in a different worksheet Excel VBA隐藏基于另一个单元格选择的行无法使代码正常工作 - Excel VBA to hide rows based on another cell selection can't get code to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM