我已经整理了一个宏,它允许我将数据从一张表存档到另一张表,但是我无法让它在之后清除信息。第一列包含我不想清除的数字,现在它只是清除 B 列中的数据。
如果有人可以看看这个,我会非常感激。
'Sub archive()
Dim i, lastrow
Dim mytext As String
lastrow = Sheets("Rooms").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow
mytext = Sheets("Rooms").Cells(i, "F").Text
If InStr(mytext, "yes") Then
Sheets("Rooms").Cells(i, "A").EntireRow.Copy Destination:=Sheets("Archive").Range("A" & Rows.Count).End(xlUp).Offset(1)
Sheets("Rooms").Cells(i, "B").Clear
End If
Next i
End Sub'
最佳答案
我已将 B 列中相关行上的单元格扩展到包含任何值的同一行上的最后一个单元格。
Sub archive()
Dim i, lastrow
Dim mytext As String
With WorkSheets("Rooms")
lastrow = .Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow
mytext = .Cells(i, "F").Text
If InStr(1, mytext, "yes", vbTextCompare) Then
.Cells(i, "A").EntireRow.Copy Destination:=Sheets("Archive").Range("A" & Rows.Count).End(xlUp).Offset(1)
.Range(.Cells(i, "B"), .Cells(i, Columns.Count).End(xlToLeft)).Clear
End If
Next i
End With
End Sub
此外,我使用了 With ... End With statement联系
WorkSheets("Rooms")
及其所有单元格,以避免重复的工作表引用。Range.Clear命令清除所有值和格式。如果您只想删除这些值,您可能需要切换到 Range.ClearContents method .
关于excel - 问: How to clear cells after archiving?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32899296/