如何find“0”值而不是空单元格

我有一个电子表格,包含8列的数据,AH,每个是不同的长度,可能包含我需要删除的值“0”。 而不是做一个循环,我试图用.Find方法来做到这一点,但我正在变得不稳定,因为find方法将空单元格(列的长度比最大长度列短)分类为零。 当函数find这些单元格中的一个时,它将select并删除该列顶部的单元格,而不是select特定的单元格。 我已经尝试使用我的代码使用0以外的值,它工作正常,所以这是一个与Excel分类空单元格为“0”的问题。

有没有一种方法来专门search值“0”? 我试过使用str(0)来指定一个string值为“0”,但得到相同的结果。 我的代码的一个子集,就是:

Row = wf.Max(Cells(Rows.Count,1).End(xlUp).Row, Cells(Rows.Count,8).End(xlUp_.Row 'Originally I had zero = 0 or "0" but I tried the following to get a string version zero = Str(0) zerofix = Right(zero,1) Do While Check_Val_Existence(zerofix,Row) = True Set aCell = ActiveSheet.Range(Cells(1,1), Cells(Row,8)).Find(What:=zerofix,LookIn:=xlValues) If Not aCell Is Nothing Then aCell.Delete Shift:=xlUp End If Loop 

哪里

 Function Check_Val_Existence(ByVal Srch, ByVal Row) As Boolean Dim rFnd As Range Set rFnd = ActiveSheet.Range(Cells(1,1), Cells(Row,8)).Find(What:=Srch) If Not rFnd Is Nothing Then Check_Val_Existence = True Else Check_Val_Existence = False End If End Function 

我宁愿不必遍历代码并依次search每一列,但是看起来我可能不得不这样做。

尝试这个

 Sheet1.Cells.Replace What:="0", Replacement:="", LookAt:=xlwhole, SearchOrder:= _ xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False 

这将清除所有具有“0”的单元格

如果你想删除“0”的单元格,那么你也可以使用.Find.FindNext 。 看到这个链接

主题 :.Find和.FindNext在Excel VBA中

链接 : http : //www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/

从该链接引用

在本教程中,我将强调如何使用。find更快的search。

.Find的语法是

(What,After,LookIn,LookAt,SearchOrder,SearchDirection,MatchCase,MatchByte,SearchFormat)

哪里

expression式(必需):是任何有效的范围对象。 所以如果我们拿上面的例子,那么范围就是Range(“A1:A”&lastRow)

什么(可选变体):是“search值”

After(可选变体)之后:您希望search开始的单元格。

LookIn(可选变体):信息的types。 (xlValues或xlFormulas)

LookAt(可选变体):可以是以下XlLookAt(常量)之一:xlWhole或xlPart。

SearchOrder(可选变体):可以是以下XlSearchOrder常量之一:xlByRows或xlByColumns。

SearchDirection:可以是这些XlSearchDirection常量之一。 xlNext默认xlPrevious

MatchCase(可选变体):为了使search区分大小写。 默认值是False。

MatchByte(可选变体):仅在您select或安装双字节语言支持时使用。 如果双字节字符只匹配双字节字符,则为真。 假使双字节字符与它们的单字节等价物匹配。

SearchFormat(可选变体):search格式。

而是与所有单元格一起工作,做预定义的工作来定义感兴趣的范围 – 也就是说在其中有数字的单元格可以作为常量或者作为公式input。 这将从search中排除您的空白代码,这将加快您的代码,并消除错误的标志。

在这个例子中, rng3返回ActiveSheet中感兴趣的单元格。 然后你会在这个范围上运行你的Find例程

 Sub LookAtZeros() Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range On Error Resume Next Set rng1 = Cells.SpecialCells(xlCellTypeConstants, xlNumbers) Set rng2 = Cells.SpecialCells(xlCellTypeFormulas, xlNumbers) On Error GoTo 0 If Not rng1 Is Nothing Then If Not rng2 Is Nothing Then Set rng3 = Union(rng1, rng2) Else Set rng3 = rng1 End If Else If Not rng2 Is Nothing Then Set rng3 = rng2 End If If Not rng3 Is Nothing Then MsgBox "Range with numeric constants and/or numeric formulae is " & rng3.Address(0, 0) Else MsgBox "no numbers", vbCritical End If End Sub 

我的build议是做一个0到= 0/0的一般替代,这应该会给出一个错误(如果需要的话,由于唯一的计算,仍然会给你一个恢复值为0的方法),然后做一个查找/select特殊错误。