我真的对编程感兴趣,并决定从VBA开始,因为我的工作。所以,我在使用代码时遇到了一些麻烦。我需要识别公式结果为TRUE的单元格,然后清除所选同一行中第一个单元格的内容。但是当我使用循环时,宏返回相同的结果3次(这是必须更改的行的确切数字)。我把我的代码放在下面。有人可以给我一些帮助吗?
感谢!!!
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
答案 0 :(得分:3)
正如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
答案 1 :(得分:2)
尝试将ClearRow分配放在循环中。
Do
ClearRow = C.Row
Cells(ClearRow, 1).Select
当你在循环之前分配ClearRow时,这意味着你每次都要清除同一个单元格的内容。
答案 2 :(得分:0)
非常感谢您的回答!!我使用了chuff编写的代码并且有效。然而,这只是一个更大的代码的一部分,它必须在许多有序的woorkbooks(1到n)中使用7张相同的东西。所以,我重复了每个woorkbook的代码,但它没有改变。一件重要的事情是“真实”是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
答案 3 :(得分:0)
谢谢你们,现在一切都很好!!!在最终代码
下面问题是在开始下一个“with”之前c变量必须是干净的。所以我已插入
每个“with”之前Set c = Nothing
,就像这样:
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