我正在尝试创建一个宏或VBA代码来检查电子表格中的符号:¤。如果它找到这个值,它需要将行中的数据变成空白,例如。它不应该完全删除该行。
我正在使用Excel2010,我们非常感谢任何帮助。
尝试编辑我找到的以下代码,但没有运气,使用范围搜索或如何将单元格留空而不是删除行。
Sub Deletesymbol()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With ActiveSheet
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Range("A:BJ")
If Not IsError(.Value) Then
If .Value = "¤" Then **.EntireRow.Delete**
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
答案 0 :(得分:2)
这应该可以解决问题:
Sub rowKiller()
Dim rng As Range, r As Range
Set rng = Nothing
For Each r In ActiveSheet.UsedRange.Cells.SpecialCells(2)
If InStr(1, r.Value, Chr(164)) > 0 Then
If rng Is Nothing Then
Set rng = r
Else
Set rng = Union(rng, r)
End If
End If
Next r
If Not rng Is Nothing Then
rng.EntireRow.Clear
End If
End Sub
这应该非常快,因为最多只执行 1 Clear
。
我假设{{1}是常数而不是公式。
答案 1 :(得分:1)
这是一个适合您的解决方案:
<div class="profile__photo">
<div class="profile__photo--border-1"></div>
<div class="profile__photo--border-2"></div>
<img class="profile__avatar" src="https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_960_720.png" alt="profile photo">
</div>
这就是它的作用:
Sub TestMe()
Dim symbol As String: symbol = Chr(164)
Dim myRow As Range
Dim myCell As Range
Dim rowToDelete As Range
For Each myRow In ActiveSheet.UsedRange.Cells.Rows
Set myCell = myRow.Find(symbol)
If Not myCell Is Nothing Then
If rowToDelete Is Nothing Then
Set rowToDelete = myRow
Else
Set rowToDelete = Union(rowToDelete, myRow)
End If
End If
Set myCell = Nothing
Next myRow
If Not rowToDelete Is Nothing Then
'you may use rowToDelete.Select to see what would be cleared
rowToDelete.EntireRow.Clear '.ClearContents is also possible
End If
End Sub
,因此您可以编写UsedRange
而不是UsedRange.Rows
。Range("A1:E10").Rows
设置为包含myCell
符号Chr(164)
rowToDelete
。如果您不想删除该样式,请写下rowToDelete
而不是.ClearContents
。答案 2 :(得分:1)
两个有效的解决方案:
如果你知道在哪里可以检查特殊字符并清除指定的行
Sub SearchForChar()
Dim maxRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1") 'Replace Sheet1 with your worksheet name
maxRow = ws.Range("A1").End(xlDown).Row
For i = 1 To maxRow
If ws.Range("A" & i).Value Like "*¤*" Then 'Replace A where you expect ¤ to be
ws.Range("A" & i).EntireRow.Value = "" 'sets value of whole row to null
End If
Next i
End Sub
检查整个工作表中的每个单元格,如果找到则清除整行 Sub SearchForChar()
'Check each cell in the whole worksheet for ¤ and removes whole row if it finds it.
Dim ws As Worksheet
Set ws = Worksheets("Sheet1") 'Replace Sheet1 with your worksheet name
For Each cell In ws.Cells
If cell.Value Like "*¤*" Then
cell.EntireRow.Value = ""
End If
Next cell
End Sub
编辑:
根据评论中的要求,您可以使用类似
的内容Sub SearchForChar()
Application.ScreenUpdating = False
'Check each cell in the whole worksheet for ¤ and removes whole row if it finds it.
Dim ws As Worksheet
Dim rng as Range
Set ws = Worksheets("Sheet1") 'Replace Sheet1 with your worksheet name
For Each rng In ws.Range("A5:B1000")
If rng.Value Like "*¤*" Then
rng.EntireRow.Value = ""
End If
Next rng
Application.ScreenUpdating = True
End Sub
答案 3 :(得分:1)
如果要从包含的任何单元格中删除该字符,则只需将Replace()方法与lookAt:=xlPart
参数一起使用:
Sub main()
With Intersect(ActiveSheet.UsedRange, Range("A:BJ"))
.Replace what:=Chr(164), lookAt:=xlPart, Replacement:=""
End With
End Sub
如果要清除其内容与匹配该字符的单元格,则仍然使用Replace()方法,但使用lookAt:=xlWhole
参数:
Sub main()
With Intersect(ActiveSheet.UsedRange, Range("A:BJ"))
.Replace what:=Chr(164), lookAt:=xlWhole, Replacement:=""
End With
End Sub
如果要清除整行中至少有一个单元格包含该字符的内容,请在循环中使用Find()
函数
Option Explicit
Sub main()
Dim found As Range, cellsToClear As Range
Dim firstAddress As String
With Intersect(ActiveSheet.UsedRange, Range("A:BJ"))
Set found = .Find(what:=Chr(164), lookAt:=xlPart, LookIn:=xlValues)
If Not found Is Nothing Then
Set cellsToClear = .Offset(, .Columns.Count).Resize(1, 1)
firstAddress = found.Address
Do
Set cellsToClear = Union(cellsToClear, found)
Set found = .FindNext(found)
Loop While found.Address <> firstAddress
Intersect(cellsToClear, Range("A:BJ")).entireRow.ClearContents 'clear the content of found cells entire row
End If
End With
End Sub
答案 4 :(得分:1)
这是Find的另一种选择:
Sub RowKiller()
Dim rng As Range
Do While True
Set rng = Cells.Find(What:="¤", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If rng Is Nothing Then
Exit Sub
End If
rng.EntireRow.Delete
Loop
End Sub