Excel 在VBA中使用find方法
我对编程非常感兴趣,并决定从VBA开始,因为我的工作。所以,我在代码方面遇到了一些问题。我需要确定公式结果为真的单元格,然后清除所选同一行中第一个单元格的内容。但当我使用loop时,宏会返回相同的结果3次(这是必须更改的行数)。我会把我的代码放在下面。有人能帮我一下吗 谢谢Excel 在VBA中使用find方法,excel,vba,Excel,Vba,我对编程非常感兴趣,并决定从VBA开始,因为我的工作。所以,我在代码方面遇到了一些问题。我需要确定公式结果为真的单元格,然后清除所选同一行中第一个单元格的内容。但当我使用loop时,宏会返回相同的结果3次(这是必须更改的行数)。我会把我的代码放在下面。有人能帮我一下吗 谢谢 Sub Teste2sigma() Windows("1.xls").Activate Sheets("Standard 1").Activate Range("AI3:AJ42").Select
Sub Teste2sigma()
Windows("1.xls").Activate
Sheets("Standard 1").Activate
Range("AI3:AJ42").Select
With Range("AI3:AJ42")
Set C = .Find("TRUE", LookIn:=xlValues)
If Not C Is Nothing Then
ClearAddress = C.Address
ClearRow = C.Row
ClearColumn = C.Column
Do
Cells(ClearRow, 1).Select
Cells(ClearRow, 1).ClearContents
ClearRows = ClearAddress & "," & C.Address(RowAbsolute:=False)
'Cells(ClearRow, ClearColumn).Select
Set C = .FindNext(After:=C)
Loop While Not C Is Nothing And C.Address <> ClearAddress
End If
End With
End Sub
Sub Teste2sigma()
Windows(“1.xls”)。激活
工作表(“标准1”)。激活
范围(“AI3:AJ42”)。选择
带范围(“AI3:AJ42”)
设置C=.Find(“TRUE”,LookIn:=xlValues)
如果不是,那么C什么都不是
ClearAddress=C.地址
ClearRow=C.行
ClearColumn=C.列
做
单元格(ClearRow,1)。选择
单元格(ClearRow,1)。ClearContent
ClearRows=ClearAddress&“,”&C.Address(RowAbsolute:=False)
'单元格(ClearRow,ClearColumn)。选择
设置C=.FindNext(之后:=C)
循环而不是C为Nothing,C为Address ClearAddress
如果结束
以
端接头
尝试将ClearRow分配放入循环中
Do
ClearRow = C.Row
Cells(ClearRow, 1).Select
此时,您正在循环之前分配ClearRow,这意味着您每次都在清除同一单元格的内容。正如Craig T所指出的,您的清除操作是错误的。我还简化了代码
Option Explicit
Sub Teste2sigma()
Dim c As Range
Dim ClearAddress As String
Dim ClearRow As Long
With ThisWorkbook.Worksheets("Sheet1").Range("AI3:AJ42")
Set c = .Find(What:=True, LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).ClearContents
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
End Sub
选项显式
子测试仪2西格玛()
调光范围
将ClearAddress设置为字符串
昏暗的净空长
使用此工作簿。工作表(“表1”)。范围(“AI3:AJ42”)
Set c=.Find(What:=True,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。ClearContent
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
端接头
非常感谢您的回答!!我使用了chuff编写的代码,它很有效。然而,这只是一个更大的代码的一部分,它必须在许多订购的woorkbooks(1到n)中对7张纸做同样的事情。因此,我重复了每个woorkbook的代码,但它没有改变任何东西。重要的一点是,“True”是excel中OR函数的结果
Sub Teste2Sigma()
Windows("datacao_v2.xls").Activate
Sheets("SamList").Activate
Range("f2").Select
varPasta = ActiveCell
varDatacao = varPasta & "datacao_v2"
Sheets("SamList").Select
Columns(1).Find(What:="fim").Activate
fim = ActiveCell.Row 'linha correspondente ao "fim"
first = Cells(4, 3)
aux = Range(Cells(fim - 3, 1), Cells(fim - 3 - 9, 1)).Find(What:="GJ", SearchDirection:=xlPrevious).Row
last = Cells(aux + 1, 3)
nInt = (fim - (fim - aux) - 3) / (first + 2)
nVal = first * nInt + last 'número total de amostras lidas
nPlan = Ceiling(nVal / 4)
media = Range(Cells(2, 1), Cells(nPlan + 1, 1))
For K = 1 To nPlan
Windows(K & ".xls").Activate
Dim c As Range
Dim ClearAddress As String
Dim ClearRow As Long
With Worksheets("Standard 1").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
With Worksheets("Standard 2").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
With Worksheets("Sample 1").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
With Worksheets("Sample 2").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
With Worksheets("Sample 3").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
With Worksheets("Sample 4").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
With Worksheets("Blank").Range("Z7:Z46")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
With Worksheets("Blank").Range("AA7:AA46")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 23).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
Next K
End Sub
Sub Teste2Sigma()
Windows(“datacao_v2.xls”)。激活
工作表(“SamList”)。激活
范围(“f2”)。选择
VARPATA=ActiveCell
varDatacao=varPasta&“datacao\u v2”
工作表(“SamList”)。选择
第(1)列。查找(内容:=“fim”)。激活
fim=ActiveCell.第行“linha通信ao”fim
第一个=单元(4,3)
aux=范围(单元格(fim-3,1),单元格(fim-3-9,1))。查找(内容:=“GJ”,搜索方向:=xlPrevious)。行
最后=单元(辅助+1,3)
nInt=(职能指令手册-(职能指令手册-辅助)-3)/(第一+2)
nVal=第一个*9个+最后一个'número总计
nPlan=天花板(nVal/4)
媒体=范围(单元格(2,1),单元格(nPlan+1,1))
对于K=1到nPlan
Windows(K&“.xls”)。激活
调光范围
将ClearAddress设置为字符串
昏暗的净空长
带有工作表(“标准1”)。范围(“AI3:AJ42”)
设置c=.Find(What:=“True”,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。Value=“”
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
带有工作表(“标准2”)。范围(“AI3:AJ42”)
设置c=.Find(What:=“True”,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。Value=“”
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
带有工作表(“样本1”)。范围(“AI3:AJ42”)
设置c=.Find(What:=“True”,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。Value=“”
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
带有工作表(“样本2”)。范围(“AI3:AJ42”)
设置c=.Find(What:=“True”,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。Value=“”
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
带有工作表(“样本3”)。范围(“AI3:AJ42”)
设置c=.Find(What:=“True”,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。Value=“”
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
带有工作表(“样本4”)。范围(“AI3:AJ42”)
设置c=.Find(What:=“True”,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。Value=“”
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
带有工作表(“空白”)。范围(“Z7:Z46”)
设置c=.Find(What:=“True”,LookIn:=xlValues,lookat:=xlPart)
如果不是,那么c什么都不是
ClearAddress=c.地址
做
ClearRow=c.行
单元格(ClearRow,1)。Value=“”
集合c=.FindNext(c)
循环而c.地址为ClearAddress
如果结束
以
带有工作表(“空白”)。范围(“AA7:AA
Set c = Nothing
Worksheets("Sample 2").Activate
With Worksheets("Sample 2").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With
Set c = Nothing
Worksheets("Sample 3").Activate
With Worksheets("Sample 3").Range("AI3:AJ42")
Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
ClearAddress = c.Address
Do
ClearRow = c.Row
Cells(ClearRow, 1).Value = ""
Set c = .FindNext(c)
Loop While c.Address <> ClearAddress
End If
End With