请想象一个由三行组成的表格:
- 第 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