[英]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
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
. 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.