简体   繁体   English

如何删除用户指定行中的特定单元格

[英]How to Delete Specific Cells in a User Specified Row

I am looking for help to delete specific cells in a row that is specified by the user ( activecell.row ).我正在寻求帮助以删除用户指定的行中的特定单元格 ( activecell.row )。 My code inserts a line below the currently selected row.我的代码在当前选定的行下方插入一行。 I would also like to delete a couple of the cells in that row without deleting the entire row and all of the formulas.我还想删除该行中的几个单元格而不删除整行和所有公式。

Sub AddNewLine()
    Dim ws As Worksheet
    Dim rng As Range
    Dim myVal As Integer

    ActiveSheet.Unprotect
    
    '~~> Set this to the relevant worksheet
    Set ws = ActiveWorkbook.ActiveSheet

    With ws
        '~~> Set your range
        Set rng = .Rows(ActiveCell.Row)

        '~~> Copy the range
        rng.Copy

        '~~> Insert the range
        rng.Offset(1).Insert Shift:=xlDown
   
        'I would like to delete the values in these cells:
            Range("G.25").ClearContents
            Range("I25").ClearContents
            Range("J25").ClearContents
            Range("K25").ClearContents

    End With
       
    ActiveSheet.Protect
    
End Sub

Thanks for your help!谢谢你的帮助!

Try this code:试试这个代码:

Sub AddNewLine()
    Dim ws As Worksheet
    Dim rng As Range
    Dim myVal As Integer

    ActiveSheet.Unprotect
    
    '~~> Set this to the relevant worksheet
    Set ws = ActiveWorkbook.ActiveSheet

    With ws
        '~~> Set your range
        Set rng = .Rows(ActiveCell.Row)

        '~~> Copy the range
        rng.Copy

        '~~> Insert the range
        rng.Offset(1).Insert Shift:=xlDown
   
        'I would like to delete the values in these cells:
        rng.Offset(1).Columns("G").ClearContents
        rng.Offset(1).Columns("I:K").ClearContents

    End With
       
    ActiveSheet.Protect
    
End Sub
Sub AddNewLine()
    Dim rng As Range
    
    With ActiveSheet
        .Unprotect
    
        '~~> Set your range
        Set rng = ActiveCell.EntireRow
        
        '~~> Copy the range
        rng.Copy
        
        '~~> Insert the range
        rng.Offset(1).Insert
        
        'I would like to delete the values in these cells:
        Intersect(.Range("G:G, I:K"), rng).ClearContents
    
        .Protect
    End With
End Sub

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

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