多行 Excel 电子表格中的特定单元格删除

多行 Excel 电子表格中的特定单元格删除

请想象一个由三行组成的表格:

  • 第 n、n+1 和 n+2 行相同
  • 行 n+3、n+4 和 n+5 相同
  • 等等

您能否帮助我提供以下代码来执行以下操作:

  • 清除第 n 行第 Q 列和第 R 列单元格的内容
  • 清除第 n+1 行第 P 列和第 R 列单元格的内容
  • 清除第 n+2 行第 P 列和第 Q 列单元格的内容
  • 对其他 3 行组重复此操作,直到结束

感谢您的帮助

感谢 Flex 建议我展示我目前所做的工作。我尝试了很多方法,但都以有限的知识完成了最后一部分,但都没有成功。我进行的其他步骤如下:

    Sub Macro11()
'
' Macro11 Macro
'
' Keyboard Shortcut: Ctrl+k
'
Dim myCell
Set myCell = ActiveCell

While ActiveCell.Value <> ""
    Rows(ActiveCell.Row).Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
    Rows(ActiveCell.Row).Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
    myCell.Offset(1, 0).Select
    Set myCell = ActiveCell
Wend
    Columns("A:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("A:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A12").Select
    ActiveCell.FormulaR1C1 = "Numeric Identifier"
    Range("C12").Select
    ActiveCell.FormulaR1C1 = "Department"
    Range("D12").Select
    ActiveCell.FormulaR1C1 = "Category"
    Range("A11:A12,B11:B12,C11:C12,D11:D12").Select
    Range("D12").Activate
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
    End With
    With Selection.Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
        Range("E1").Select
    Selection.Copy
    Range("B13").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("E2").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("C13").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("E9").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("D13").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

这是我最新尝试的:

Sub Macro13()
'
' Macro13 Macro
'
' Keyboard Shortcut: Ctrl+h
'
While ActiveCell.Value <> ""
Dim myCell
Set myCell = ActiveCell
Do
 Range("Q").Value = 0
 Range("R").Value = 0

myCell.Offset(1, 0).Select
Set myCell = ActiveCell

 Range("P").Value = 0
 Range("R").Value = 0
 
myCell.Offset(1, 0).Select
Set myCell = ActiveCell

 Range("P").Value = 0
 Range("Q").Value = 0
 
myCell.Offset(1, 0).Select
Set myCell = ActiveCell
Loop
Wend


End Sub

再次 => 反复试验。从来没有时间好好学习如何做 vba

答案1

您可以使用类似的东西。请阅读评论并根据需要进行调整。

这里要学习的最佳技巧是For循环可以Step包含一个参数。

Option Explicit


Public Sub ClearCells()


Dim lastRow As Integer

Dim i As Integer

Dim r As Range

Dim ws As Worksheet

Set ws = ActiveSheet

'use this if every row has a value in column A
lastRow = ws.Range("A1").End(xlDown).Row

'OR use this to just specify the last row with data (if you prefer)
'lastRow = 10

'Change the starting row of your data by changing A2 to whatever is the top-left cell you have data in
'also change the Z to the right-most column you have data in
Set r = ws.Range("A2:Z" & lastRow)

'loop through each set of three rows in the range
For i = 1 To r.Rows.Count Step 3

 With r
    'clear Q and R
    .Cells(i, 17).ClearContents
    .Cells(i, 18).ClearContents
    
    'clear P and R on i + 1
    .Cells(i + 1, 16).ClearContents
    .Cells(i + 1, 18).ClearContents
    
    'clear P and Q on i + 2
    .Cells(i + 2, 16).ClearContents
    .Cells(i + 2, 17).ClearContents
    
 End With

Next i 'steps three rows down

MsgBox "Finished clearing cells"

End Sub

在此处输入图片描述

答案2

再次感谢您的代码:非常有帮助。我今天早上玩了一下,并更改了您建议需要适合我的电子表格的区域。现在它看起来是这样的:在此处输入图片描述

上面两行来自上一个宏。我有两个问题:

  1. 我不太熟悉您建议的宏所启动的“选项显式”命令,但每次我将它放在宏的开头时,该行代码最终都会移动到前一个宏的末尾:这是正常的吗?
  2. 正如您所见,我将运行您的宏与“Ctrl + q”按键关联起来:由于某种原因,这不起作用(总是与我的其他宏一起使用)但如果我打开宏编辑器并从那里“运行”宏=>它做得很好=>先生/女士,谢谢您!

非常感谢您花时间帮助我。谢谢

相关内容