Excelmacros删除单元格的模式

我需要帮助创build一个macros,当站在单元格A时,它将删除下面标记为X的单元格中的值(将单元格标记为O)。 实际的单元格可以包含任何值。

AXXX XOXX XXOX XXXO 

这可能吗?

试试这个代码:

 Sub go_sub() Dim tmpRNG As Range Set tmpRNG = ActiveCell.CurrentRegion 'or you could set other range definition here, like Range("A1:d4") Dim cell As Range For Each cell In tmpRNG If cell.Row <> cell.Column Then cell.ClearContents Next cell End Sub 

编辑上面的代码适用于在单元格A1中开始的当前区域。

下面的代码适用于任何选定的区域:

 Sub go_sub() Dim tmpRNG As Range Set tmpRNG = Selection Dim tmpOff As Long tmpOff = tmpRNG.Row - tmpRNG.Column Dim cell As Range For Each cell In tmpRNG '.Cells If cell.Row - tmpOff <> cell.Column Then cell.ClearContents Next cell End Sub 

一个提示:如果你有一个大的做closures屏幕更新的工作,也可能closures事件。

下面将把你当前的单元格,并删除所有,但对angular线,select左上angular的单元格,所有的对angular线将保持…但我喜欢KazJaw的答案。

 Sub go_sub() 'get the range from current cell to the end Lastrow = ActiveSheet.Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row Lastcol = ActiveSheet.Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column Set myRange = ActiveSheet.Range(Selection, ActiveSheet.Cells(Lastrow, Lastcol)) 'set the next cell to keep as current one Set Nextcell = selection 'cycle throug all cells in the range For Each cel In myRange 'if the cell is to be kept? if cel.address = nextcell.address then 'Reset the next cell to save BUT DONE CLEAR THECURRENT CELL set Nextcell = Nextcell.offset(1,1) Else 'clear current cell if not to be saved Cel.clearcontents End if Next End Sub 

之前:

之前

后:

后