macros不会删除所有空白单元格

这是一个冗长的查询,因为我包含失败的代码。

问题:当我完成将文本文件导入为excel时,我的工作表包含一些“空白”单元格。 我已经在其他地方成功地使用了下面的代码,但在这个场合它不适合我。

Range("b1:AZ60").Select Selection.SpecialCells(xlCellTypeBlanks).Select Selection.Delete shift:=xlToLeft 

我search了networking,发现了一些build议的解决scheme(如下)。 我已经试过在我的代码之前运行下面的代码4 snipets每个清除所谓的空白单元格的内容,但没有任何工作到目前为止。

1号 – – – – – – – – – – – – – – – – – – – – – – – – ———

  Set rng = Intersect(Selection, Selection.Parent.UsedRange) For Each C In rng If Trim(C) = "" Then C.ClearContents End If Next C 

我已经用c.valuereplace了上面的修剪,但没有

 If C.Value <> "" Then 

2号 – – – – – – – – – – – – – – – – – – – – – – – – –

 For Each aCell In ActiveSheet.Cells.SpecialCells(xlCellTypeConstants) If Not aCell.Value Like "*[! ]*" Then aCell.ClearContents Next 

第3号———————————————— –

 For Each C In rng If IsEmpty(C) Then C.Delete shift:=xlToLeft Else ActiveCell.Select End If Next C 

4号————————————–

最后,我发现这个干净的function,但似乎没有办法。

 Set rng = Intersect(Selection, Selection.Parent.UsedRange) For Each C In rng If Not IsError(C) Then C.Value = MEGACLEAN(C) End If Next C ' ' ' End Sub ----------------------------------------- Function MEGACLEAN(varVal As Variant) Dim NewVal As Variant If IsMissing(varVal) Then Exit Function NewVal = Trim(varVal) 'remove spaces NewVal = Application.WorksheetFunction.Clean(NewVal) 'remove most unwanted characters NewVal = Application.WorksheetFunction.Substitute(NewVal, Chr(127), "") 'remove ASCII#127 NewVal = Application.WorksheetFunction.Substitute(NewVal, Chr(160), "") 'remove ASCII#160 MEGACLEAN = NewVal End Function 

也许这与我从中导入的文本文件有关,但是这些文件都没有奏效,因为我还有一些空白的单元格。 任何帮助将非常感激!!

不知道如果我应该回答我自己的这个问题,但是我混淆了我上面的代码的位,并与我find了一个mcaro混合,并提出了以下代码是很慢,但似乎做我的诡计。 感谢所有的input,虽然!

  Set rangetext = Cells.SpecialCells( _ xlCellTypeConstants, _ xlTextValues) For Each rangesheet In rangetext If Trim(rangesheet.Value) = "" Then rangesheet.ClearContents End If Next Set rangetext = Nothing Set rangesheet = Nothing Range("b1:AZ60").Select Set rng = Intersect(Selection, Selection.Parent.UsedRange) For Each C In rng If IsEmpty(C) Then C.FormulaR1C1 = "=0" If C.Value = 0 Then C.ClearContents C.Select Selection.SpecialCells(xlCellTypeBlanks).Select Selection.Delete shift:=xlToLeft End If End If Next C 

找出你在这些“空白”单元中有哪些字符。

select一个单元格并运行下面的GetSelectionContentsmacros:

 Sub GetSelectionContents() MsgBox sAnalyseString(selection) End Sub Function sAnalyseString(sSTR As String) As String Dim lLoop As Long, sTemp As String For lLoop = 1 To Len(sSTR) sTemp = sTemp & ", " & Asc(Mid(sSTR, lLoop, 1)) Next sAnalyseString = Mid(sTemp, 2) End Function 

然后,一旦你有你的神秘人物,在运行你的删除macros之前,将其replace。

Activesheet.usedrange.Replace What:=chr(32), Replacement:="", LookAt:=xlPart, _ SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False