将指定的VBA Excel逻辑应用于所选行。

huangapple go评论62阅读模式
英文:

Apply specified VBA excel logic to the selected row

问题

我有一个问题,我需要你的帮助。

我在VBA Excel中是新手,但我卡在了以下宏:

我想要重复以下操作,但针对选定的行:

假设我这里选定的行是:“2”,它被复制并粘贴两次到“第3行和第4行”。

然后,对于第2行,删除单元格的数据(C2和D2)
然后对于第3行,删除单元格的数据(B3和D3)
然后对于第4行,删除单元格的数据(B4和C4)

当我对这行应用宏时,我得到了以下逻辑:

```vba
Sub test()
'
' test 宏
'
    Rows("2:2").Select
    Selection.Copy
    Rows("3:3").Select
    Selection.Insert Shift:=xlDown
    Rows("2:2").Select
    Application.CutCopyMode = False
    Selection.Copy
    Rows("4:4").Select
    Selection.Insert Shift:=xlDown
    Application.CutCopyMode = False
    Range("C2:D2").Select
    Selection.ClearContents
    Range("B3,D3").Select
    Range("D3").Activate
    Selection.ClearContents
    Range("B4:C4").Select
    Selection.ClearContents

End Sub

任何帮助?


<details>
<summary>英文:</summary>

I have an issue, I need your help with me.

I&#39;m new in VBA excel, but I stuck with the following macro:

I want to repeat the following action, but for selected row:

Let&#39;s say the selected row in my case is : &quot;2&quot;, its copied and paste two times down &quot;row 3 and 4&quot;

Then, for row 2, delete data of cells (C2 and D2)
Then for row 3, delete data of cells (B3 and D3)
Then for row 4, delete data of cells (B4 and C4)

when I applied macro for this row, I resulted the following logic:

Sub test()
'
' test Macro
'
Rows("2:2").Select
Selection.Copy
Rows("3:3").Select
Selection.Insert Shift:=xlDown
Rows("2:2").Select
Application.CutCopyMode = False
Selection.Copy
Rows("4:4").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Range("C2:D2").Select
Selection.ClearContents
Range("B3,D3").Select
Range("D3").Activate
Selection.ClearContents
Range("B4:C4").Select
Selection.ClearContents

End Sub


Any help?

To get a proper logic solve my issue.

</details>


# 答案1
**得分**: 3

这是您提供的代码的翻译:

```vbnet
插入和清除
-

<!-- 语言: lang-vb -->

子插入并清除()

    Dim ClearCols(): ClearCols = VBA.Array( _
        VBA.Array(3, 4), VBA.Array(2, 4), VBA.Array(2, 3))

    If Selection Is Nothing Then Exit Sub
    If Not TypeOf Selection Is Range Then Exit Sub

    Dim MaxColumns As Long, cMax As Long, n As Long

    对于 n = LBound(ClearCols) 到 UBound(ClearCols)
        cMax = Application.Max(ClearCols(n))
        如果 cMax > MaxColumns Then
            MaxColumns = cMax
        End If
    下一个 n

    与 Selection.Rows(1) 一起
        Dim cCount As Long: cCount = .Columns.Count
        如果 cCount < MaxColumns Then Exit Sub
        .Offset(1).Resize(2).Insert Shift:=xlShiftDown
        .Copy .Offset(1).Resize(2)
        对于 n = LBound(ClearCols) 到 UBound(ClearCols)
            .Cells(n + 1, ClearCols(n)(0)).ClearContents
            .Cells(n + 1, ClearCols(n)(1)).ClearContents
        下一个 n
    结束与

End Sub

这是代码的中文翻译部分。如果您需要更多帮助,请随时告诉我。

英文:

Insert & Clear

<!-- language: lang-vb -->

Sub InsertAndClear()
    
    Dim ClearCols(): ClearCols = VBA.Array( _
        VBA.Array(3, 4), VBA.Array(2, 4), VBA.Array(2, 3))
    
    If Selection Is Nothing Then Exit Sub
    If Not TypeOf Selection Is Range Then Exit Sub
    
    Dim MaxColumns As Long, cMax As Long, n As Long
    
    For n = LBound(ClearCols) To UBound(ClearCols)
        cMax = Application.Max(ClearCols(n))
        If cMax &gt; MaxColumns Then
            MaxColumns = cMax
        End If
    Next n
    
    With Selection.Rows(1)
        Dim cCount As Long: cCount = .Columns.Count
        If cCount &lt; MaxColumns Then Exit Sub
        .Offset(1).Resize(2).Insert Shift:=xlShiftDown
        .Copy .Offset(1).Resize(2)
        For n = LBound(ClearCols) To UBound(ClearCols)
            .Cells(n + 1, ClearCols(n)(0)).ClearContents
            .Cells(n + 1, ClearCols(n)(1)).ClearContents
        Next n
    End With
        
End Sub

答案2

得分: 1

Selection.Row 属性返回当前选定的行。使用此作为起点,以下代码应执行您需要的步骤。

Sub test()
    Dim selectedRow As Integer
    selectedRow = Selection.Row
    
    Rows(selectedRow).Copy
    Rows(selectedRow + 1).Insert Shift:=xlDown
    Rows(selectedRow).Copy
    Rows(selectedRow + 2).Insert Shift:=xlDown
    
    Range("C" & selectedRow & ":D" & selectedRow).ClearContents
    Range("B" & (selectedRow + 1) & ", D" & (selectedRow + 1)).ClearContents
    Range("B" & (selectedRow + 2) & ":C" & (selectedRow + 2)).ClearContents
End Sub
英文:

The Selection.Row property returns the currently selected row. Using this as starting point, the following code should perform the steps you need.

Sub test()
    Dim selectedRow As Integer
    selectedRow = Selection.Row
    
    Rows(selectedRow).Copy
    Rows(selectedRow + 1).Insert Shift:=xlDown
    Rows(selectedRow).Copy
    Rows(selectedRow + 2).Insert Shift:=xlDown
    
    Range(&quot;C&quot; &amp; selectedRow &amp; &quot;:D&quot; &amp; selectedRow).ClearContents
    Range(&quot;B&quot; &amp; (selectedRow + 1) &amp; &quot;, D&quot; &amp; (selectedRow + 1)).ClearContents
    Range(&quot;B&quot; &amp; (selectedRow + 2) &amp; &quot;:C&quot; &amp; (selectedRow + 2)).ClearContents
End Sub

huangapple
  • 本文由 发表于 2023年5月21日 20:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定