我对整个Visual Basic场景比较陌生,但对编程有基本知识,我理解如何阅读代码并遵循逻辑模式。
我想在这个宏中完成的工作如下:
在电子表格中找到“禁止”一词的行,然后删除该行和以下行,直到找到其中显示“其他响应类别”的行,并在此处停止而不从行中删除该行。 / p>
在电子表格中找到“需要质询响应”字样的行并删除该行及其下方的所有行,直到找到一行中有一行名为“Tracking Links Clicked”的行并停在那里不删除该行。
在电子表格中找到显示“链接名称(HTML)”字样的行,并删除该行及其下方的所有行。
我已经使用“记录宏”功能来了解如何从excel中删除文本行,但只能使用选中然后删除的范围区域;不搜索关键短语。
我正在研究很多VB的东西,以便真正写出我想要完成的东西。
Edit2:所以我修改并简化了你提供的VB代码,至少尝试并获得你提供的相同响应;但只搜索其中一个值。我想在尝试添加更多内容之前获得一个值的正确输入。
编辑3:能够在朋友的帮助下编写脚本,非常感谢大家的投入。我在这里附上了工作脚本:
Option Explicit
Sub Autoformat()
Dim WSA As Worksheet
Dim Rng1 As Range
Dim Rng2 As Range
Dim lpRange As Range
Dim sArr As Variant
Set WSA = ActiveSheet
Dim i As Long
sArr = Array("Suppressed", "Other Response Categories", "Requires Challenge Response", "Tracking Links Clicked", "Link Name (HTML)")
Rows("6:9").Delete
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
For i = 0 To 3 Step 2
Set lpRange = WSA.UsedRange
Set Rng1 = lpRange.Find(What:=sArr(i), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
MatchCase:=False)
Set Rng2 = lpRange.Find(What:=sArr(i + 1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
MatchCase:=False)
On Error Resume Next
If Not Rng1 Is Nothing And Not Rng2 Is Nothing And Rng2.Row > Rng1.Row Then
WSA.Rows(Rng1.Row & ":" & Rng2.Row - 1).Delete
ElseIf Not Rng1 Is Nothing And Rng2 Is Nothing Then
WSA.Rows(Rng1.Row).Delete
End If
Next i
Set lpRange = WSA.UsedRange
Set Rng2 = lpRange.Find(What:=sArr(i), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
MatchCase:=False)
If Not Rng2 Is Nothing Then
WSA.Rows(Rng2.Row & ":" & Rows.Count).Clear
End If
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
Sub ClearNames()
Dim n As Name
For Each n In ThisWorkbook.Names
n.Delete
Next n
End Sub
答案 0 :(得分:2)
宏记录器是发现您不知道的语句语法的好方法。但是,宏记录器不知道您的目标;它只记录你的每一个动作。结果需要大量整理。
如果您要发布问题并希望了解答案,必须学习Excel VBA。在网上搜索“Excel VBA教程”。有很多可供选择,所以选择一个符合你的学习风格。我更喜欢访问一个大型图书馆并试用他们拥有的VB Excel引物。然后我买了我喜欢的那个。
这是为了让您开始整理使用Macro Recorder创建的内容。
我将关键短语放在工作表中的随机单元格中,然后依次搜索它们。宏录制器的输出是:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 07/05/2014 by Tony Dallimore
'
Cells.Find(What:="Suppressed", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Other Response Categories", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Requires Challenge Response", After:=ActiveCell, LookIn _
:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Link Name (HTML)", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
End Sub
此代码查找四个短语但不执行任何其他操作。这显示了Find
方法的语法。我们需要整理此代码并保存行号而不是激活(选择)单元格。我没有尝试解释每个更改,而是从上面的代码中创建了下面的代码。研究差异并尝试理解我所做的和为什么。如有必要,请回答问题,但是你可以自己实现的越多,你的技能建立起来就越快。
Sub Demo()
Dim Rng As Range
Dim RowSupp As Long
Dim RowOther As Long
Dim RowReq As Long
Dim RowLink As Long
With Worksheets("Sheet1")
Set Rng = .Cells.Find(What:="Suppressed", After:=.Cells(Rows.Count, Columns.Count), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
' Note in the above statement I have replaced "ActiveCell" with ".Cells(Rows.Count, Columns.Count)"
' which is the bottom right cell. Find does not look at the start cell, it wraps and starts
' searching from A1. I have also replaced "xlPart" with "xlWhole".
If Rng Is Nothing Then
Call MsgBox("""Suppressed"" not found", vbOKOnly)
Exit Sub
End If
RowSupp = Rng.Row
Set Rng = .Cells.Find(What:="Other Response Categories", After:=.Cells(Rng.Row, Rng.Column), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
Call MsgBox("""Other Response Categories"" not found", vbOKOnly)
Exit Sub
End If
RowOther = Rng.Row
Set Rng = .Cells.Find(What:="Requires Challenge Response", After:=.Cells(Rng.Row, Rng.Column), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
Call MsgBox("""Requires Challenge Response"" not found", vbOKOnly)
Exit Sub
End If
RowReq = Rng.Row
Set Rng = .Cells.Find(What:="Link Name (HTML)", After:=.Cells(Rng.Row, Rng.Column), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
Call MsgBox("""Requires Challenge Response"" not found", vbOKOnly)
Exit Sub
End If
RowLink = Rng.Row
End With
Debug.Print """Suppressed"" found on row " & RowSupp
Debug.Print """Other Response Categories"" found on row " & RowOther
Debug.Print """Requires Challenge Response"" found on row " & RowReq
Debug.Print """Link Name (HTML)"" found on row " & RowLink
End Sub
上面的宏找到了这四行,并证明它已经通过将它们的值输出到立即窗口来找到它们。
对于我的虚拟工作表,输出为:
"Suppressed" found on row 6
"Other Response Categories" found on row 11
"Requires Challenge Response" found on row 16
"Link Name (HTML)" found on row 22
如果我不确定自己在做什么,我总是以这种方式编码。我确定了第1步并编写了一个例程来实现第1步。然后我确定了第2步并更新了我的代码以实现它。
如果我的数据与您的数据匹配,则需要删除第6行到第10行。
您已发布:
Rows("6:9").Select
Selection.Delete Shift:=xlUp
如果我们整理一下,我们首先得到:
.Rows("6:9").Delete Shift:=xlUp
下一步是用宏Demo
发现的行号替换6和9:
.Rows(RowSupp & ":" & RowOther - 1).Delete Shift:=xlUp
将其放在RowOther = Rng.Row
下并再次运行Demo。
删除第一批行。
第3步是考虑如何调整第三个Find
语句。当前宏依赖于RowOther
在查找2和3之间不移动。但它已经被删除的行数上移了。您不能使用.Cells(Rng.Row, Rng.Column)
作为查找3的起点。
我让你想一想从哪里开始寻找3。